gpt4 book ai didi

c++ - 这个表达式是一个 xvalue 吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:15:31 25 4
gpt4 key购买 nike

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++ - 这个表达式是一个 xvalue 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52030806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com