临时对象的数据成员在C ++ 11中是xvalue吗? [英] Is a data member of a temporary object an xvalue in C++11?

查看:1095
本文介绍了临时对象的数据成员在C ++ 11中是xvalue吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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>&&)?

Does C++11 guarantee f(A().coll) will call void f(vector<int>&&)?

推荐答案

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

Yes. C++14 standard, §5.2.5/4.2, given E1.E2:

如果E2是非静态数据成员,并且E1的类型为" cq1 vq1 X ",而E2的类型为" cq2 vq2 T" ",该表达式指定第一个表达式指定的对象的命名成员.如果E1是一个左值,则E1.E2是一个左值; 否则E1.E2是一个xvalue.

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将其归类为prvalue,但是这种分类是没有意义的,因此将其更改.但是,如果更改是由缺陷报告应用的,则该更改具有追溯力-已发布的C ++ 11标准文档N3290是错误的,而C ++ 14文档则定义了C ++ 11.可能是这种情况,否则将要求编译器在-std=c++11-std=c++14之间实现行为上的细微差别.我现在懒得搜索DR.

Pedantically, originally C++11 classified this as a prvalue, but such classification was meaningless so it was changed. If the change was applied by a defect report, though, then it's retroactive — the published C++11 standard document N3290 is wrong and the C++14 document defines C++11 instead. That's likely to be the case, as otherwise would require compilers to implement a subtle difference in behavior between -std=c++11 and -std=c++14. I'm lazy to search through DRs right now.

这篇关于临时对象的数据成员在C ++ 11中是xvalue吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆