Assigning this pointer to rvalue reference to a pointer

前端 未结 2 2078
感情败类
感情败类 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. [...]

    0 讨论(0)
  • 2020-12-16 16:58

    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.)

    0 讨论(0)
提交回复
热议问题