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. [...]
Yes, that should compile.
It is incorrect to implement this as cv T* const (where cv is the cv-qualifiers for the function, if any, and T is the class type). this is not const, merely a prvalue expression of a built-in type (not modifiable).
Many people think that because you can't modify this it must be const, but as Johannes Schaub - litb once commented long ago, a much better explanation is something like this:
// by the compiler
#define this (__this + 0)
// where __this is the "real" value of this
Here it's clear that you can't modify this (say, this = nullptr), but also clear no const is necessary for such an explanation. (And the value you have in your constructor is just the value of the temporary.)