#include <iostream>
using namespace std;
struct A
{
A()
: _p(new int(1))
{}
~A()
{
*_p = 0;
delete _p;
_p = nullptr;
}
int* _p;
};
int main()
{
//
// Let r_to_a reference to a temporary object
//
A& r_to_a = A();
//
// Is r_to_a still valid now?
//
cout << *r_to_a._p << endl; // Output : 1 instead of a run-time error
}
As I have been knowing, non-const referencing to a temporary object is ill-formed. However, the code above shows it seems legal in C++. Why?
My compiler is VC++ 2013.