C ++, what is the * this value category?

Section 9.3.2.1 of the C ++ standard states the following:

In the body of a non-static (9.3) member function, the keyword this is a prvalue expression whose value is the address of the object for which the function is called. The type of this in a member function class X is X *. If a member function is declared const, the type is const X *, if the member function is declared volatile, the type of that is volatile X *, and if the member function is declared const volatile, the type is const volatile X *.

So if this

is a prvalue, what is the category of the values *this

? The following assumes that even when the object is an rvalue, *this

it is always an lvalue. It's right? Please refer to the standard if possible.

struct F;
struct test
{
    void operator()(F &&) { std::cout << "rvalue operator()" << std::endl; }
    void operator()(F const &&) { std::cout << "const rvalue operator()" << std::endl; }
    void operator()(F &) { std::cout << "lvalue operator()" << std::endl; }
    void operator()(F const &) { std::cout << "const lvalue operator()" << std::endl; }
};

struct F
{
    void operator ()()
    {
        struct test t;
        t(*this);
    }
};

int main()
{
    struct F f;
    f();
    std::move(f)();
}

      

Output:

lvalue operator()
lvalue operator()

      

+3


source to share


1 answer


From [basic.lval]:

The l value (so called historically, since lvalues ​​can appear on the left side of an expression assignment) denotes a function or object. [Example: if E

is an expression of type pointer, then *E

is an lvalue expression referring to the object or function to which it points E

. As another example, the result of a call to a function whose return type is an lvalue reference is an lvalue. -end example]

And from [expr.unary.op]:

The unary operator *

performs indirect direction: the expression to which it applies must be a pointer to an object type or a pointer to a function type, and the result is an lvalue referencing the object or function that the expression points to.



Pointer dereferencing is an lvalue. Thus, *this

is an lvalue.

Alternatively, anything that is not an lvalue is an r value. R value:

The value of r (so called historically, since rvalues ​​can appear on the right side of an expression statement) is an x ​​value, a temporary object (12.2), or its subobject, or a value that is not associated with an object.

And *this

certainly none of these things.

+3


source







All Articles