gpt4 book ai didi

c++ - 临时对象的数据成员是 C++11 中的 xvalue 吗?

转载 作者:IT老高 更新时间:2023-10-28 23:01:09 26 4
gpt4 key购买 nike

#include <vector>

using namespace std;

struct A
{
vector<int> coll;
};

void f(const vector<int>&){}
void f(vector<int>&&){}

int main()
{
f(A().coll); // Is "A().coll" an xvalue?
}

C++11是否保证f(A().coll)将调用 void f(vector<int>&&) ?

最佳答案

是的。 C++14 标准,§5.2.5/4.2,给定 E1.E2:

If E2 is a non-static data member and the type of E1 is “cq1 vq1 X”, and the type of E2 is “cq2 vq2 T”, the expression designates the named member of the object designated by the first expression. If E1 is an lvalue, then E1.E2 is an lvalue; otherwise E1.E2 is an xvalue.

学究式地,最初 C++11 将其归类为纯右值,但这样的分类毫无意义,因此被更改。但是,如果更改是由缺陷报告应用的,则它具有追溯力——已发布的 C++11 标准文档 N3290 是错误的,而 C++14 文档定义了 C++11。很可能是这种情况,否则将需要编译器在 -std=c++11-std=c++14 之间实现细微的行为差异。我现在懒得搜索 DR。

关于c++ - 临时对象的数据成员是 C++11 中的 xvalue 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42444262/

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