这个表达式是一个 xvalue 吗?
Is this expression an xvalue?
C++ 标准对 'xvalues' (N4762 § 7.2.1.4) 的描述如下:
An expression is an xvalue if it is:
- . . .
- a class member access expression designating a non-static data member of non-reference type in which the object expression is an xvalue
考虑以下代码片段(使用 Boost 打印表达式的类型):
#include <iostream>
#include <boost/type_index.hpp>
using boost::typeindex::type_id_with_cvr;
struct X {
int var;
} x;
int main()
{
auto extended_type = type_id_with_cvr<decltype( std::move(x).var )>();
std::cout << extended_type.pretty_name() << std::endl;
}
我的问题是关于表达式 std::move(x).var
:
根据标准中的文本,我希望表达式是一个 xvalue,但输出是 int
,而不是 int &&
。
我在这里错过了什么?
My question is about the expression: std::move(x).var
Based on the text in the standard, I expect the expression to be an xvalue,
是。
but the output is int
, not int &&
那是因为 decltype
有两种形式。它可以提供有关名称声明方式的信息,也可以提供有关表达式的类型和类别的信息。
由于 std::move(x).var
是会员访问权限,您得到的是前者。要获得后者,请使用 decltype((std::move(x).var))
(带双括号)。
C++ 标准对 'xvalues' (N4762 § 7.2.1.4) 的描述如下:
An expression is an xvalue if it is:
- . . .
- a class member access expression designating a non-static data member of non-reference type in which the object expression is an xvalue
考虑以下代码片段(使用 Boost 打印表达式的类型):
#include <iostream>
#include <boost/type_index.hpp>
using boost::typeindex::type_id_with_cvr;
struct X {
int var;
} x;
int main()
{
auto extended_type = type_id_with_cvr<decltype( std::move(x).var )>();
std::cout << extended_type.pretty_name() << std::endl;
}
我的问题是关于表达式 std::move(x).var
:
根据标准中的文本,我希望表达式是一个 xvalue,但输出是 int
,而不是 int &&
。
我在这里错过了什么?
My question is about the expression:
std::move(x).var
Based on the text in the standard, I expect the expression to be an xvalue,
是。
but the output is
int
, notint &&
那是因为 decltype
有两种形式。它可以提供有关名称声明方式的信息,也可以提供有关表达式的类型和类别的信息。
由于 std::move(x).var
是会员访问权限,您得到的是前者。要获得后者,请使用 decltype((std::move(x).var))
(带双括号)。