Should the following sample compile?
struct B;
struct A
{
A(B*&&){}
};
struct B : A
{
B() : A(this){}
};
int main(){}
On LWS
I say clang is right - the code should compile. For some reason, GCC considers the this pointer to be const despite the following:
The type of
thisin a member function of a classXisX*. If the member function is declaredconst, the type of this isconst X*, if the member function is declaredvolatile, the type of this isvolatile X*, and if the member function is declaredconst volatile, the type of this isconst volatile X*.
So in this case, this should be a prvalue B* and perfectly bindable to B*&&. However, note that when binding this to an rvalue reference, the value of this will be copied into a temporary object and the reference will instead be bound to that. This ensures that you never actually modify the original this value.
A reference to type "cv1
T1" is initialized by an expression of type "cv2T2" as follows:
[...]
[...] or the reference shall be an rvalue reference.
If the initializer expression
is an xvalue, class prvalue, array prvalue or function lvalue and [...], or
has a class type (i.e., T2 is a class type), [...]
then [...]
Otherwise, a temporary of type “cv1 T1” is created and initialized from the initializer expression using the rules for a non-reference copy-initialization (8.5). The reference is then bound to the temporary. [...]