Here is the minimal example:
class A
{
A* const& this_ref;
public:
A() : this_ref(this) {}
};
GCC 5.3.0 gives warning:
warning: a temporary bound to 'A::this_ref' only persists until the constructor exits [-Wextra] A() : this_ref(this) {}
Is this
a temporary then? What the... MSVC 2015 is silent about this, and referring to class members by this_ref->member
outside the constructor in my case gives expected behaviour (but might be just a case of UB, not sure).
EDIT:
Note this question extends one linked as possible duplicate, because it's not generic question about way to create such reference, but about warning GCC (and possible other compilers other than MSVC) produces when creating one.
You are creating a dangling reference. Your code is no different from this code:
There is no "object" called
this
.this
is a keyword, and when used as an expression, it is a prvalue (a temporary) containing the address of the current instance.Here's another example of the creation of a similarly dangling reference from something that looks like an object but isn't:
Here,
a
is a named entity (an lvalue), but in the initializer ofr
, the expressiona
is a prvalue (namely the result of the decay of the array).The overall message is that you should be careful with the language feature that allows const lvalue references to bind to rvalues. Its main purpose is to make function calls easy, but its other uses are much hairier.
To be precise,
this
is not temporary, but a temporary is created here.Firstly,
this
is prvalue,Secondly, temporary object will be created when binding reference to a prvalue,
That's why GCC gives warning, because
this_ref
is bound to a temporary created. (And then become dangled later, which leads to UB.)