C++ *this 的值类别是什么?
C++ what is the value category of *this?
C++ 标准第 9.3.2.1 节指出:
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 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*.
那么如果 this
是一个纯右值,那么 *this
的值类别是什么?以下建议即使对象是右值,*this
也始终是左值。这个对吗?如果可能,请参考标准。
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)();
}
输出:
lvalue operator()
lvalue operator()
来自[basic.lval]:
An lvalue (so called, historically, because lvalues could appear on the left-hand side of an assignment
expression) designates a function or an object. [ Example: If E
is an expression of pointer type, then
*E
is an lvalue expression referring to the object or function to which E
points. As another example,
the result of calling a function whose return type is an lvalue reference is an lvalue. —end example ]
来自[expr.unary.op]:
The unary *
operator performs indirection: the expression to which it is applied shall be a pointer to an
object type, or a pointer to a function type and the result is an lvalue referring to the object or function
to which the expression points.
取消引用指针是左值。所以 *this
是一个左值。
或者,任何不是左值的都是右值。右值为:
An rvalue (so called, historically, because rvalues could appear on the right-hand side of an assignment
expression) is an xvalue, a temporary object (12.2) or subobject thereof, or a value that is not associated
with an object.
而*this
绝对是那些东西中的none。
C++ 标准第 9.3.2.1 节指出:
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 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*.
那么如果 this
是一个纯右值,那么 *this
的值类别是什么?以下建议即使对象是右值,*this
也始终是左值。这个对吗?如果可能,请参考标准。
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)();
}
输出:
lvalue operator()
lvalue operator()
来自[basic.lval]:
An lvalue (so called, historically, because lvalues could appear on the left-hand side of an assignment expression) designates a function or an object. [ Example: If
E
is an expression of pointer type, then*E
is an lvalue expression referring to the object or function to whichE
points. As another example, the result of calling a function whose return type is an lvalue reference is an lvalue. —end example ]
来自[expr.unary.op]:
The unary
*
operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points.
取消引用指针是左值。所以 *this
是一个左值。
或者,任何不是左值的都是右值。右值为:
An rvalue (so called, historically, because rvalues could appear on the right-hand side of an assignment expression) is an xvalue, a temporary object (12.2) or subobject thereof, or a value that is not associated with an object.
而*this
绝对是那些东西中的none。