Assigning this pointer to rvalue reference to a pointer

前端 未结 2 2080
感情败类
感情败类 2020-12-16 16:38

Should the following sample compile?

struct B;
struct A
{
  A(B*&&){}
};

struct B : A
{
  B() : A(this){}
};

int main(){}

On LWS

2条回答
  •  生来不讨喜
    2020-12-16 16:54

    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 this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const 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 "cv2 T2" 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. [...]

提交回复
热议问题