RVO是否在对象成员上工作? [英] Does RVO work on object members?

查看:113
本文介绍了RVO是否在对象成员上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下内容:

struct A { /* ... */ };

A foo() {
  auto p = std::make_pair(A{}, 2);
  // ... do something
  return p.first;
}

auto a = foo();

将复制,移动 p.first 或RVO-ed?

Will p.first be copied, moved or RVO-ed?

推荐答案

我发现在Visual Studio 2010和gcc-5.1 RVO是 / strong>已应用(请参阅 http://coliru.stacked-crooked.com/a/17666dd9e532da76

I've found in Visual Studio 2010 and in gcc-5.1 RVO is not applied (see for example http://coliru.stacked-crooked.com/a/17666dd9e532da76).

标准的相关部分为12.8.31.1 [class.copy]。它声明允许复制检测(我突出显示):

The relevant section of the standard is 12.8.31.1 [class.copy]. It states that copy elision is permitted (my highlighting):


在一个函数中的return语句中,是具有相同类型(忽略cv)的非易失性自动对象的名称(除了函数参数或由处理程序([except.handle])的异常声明引入的变量) -qualification)作为函数返回类型,可以通过将自动对象直接构造为函数的返回值来省略复制/移动操作

in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function parameter or a variable introduced by the exception-declaration of a handler ([except.handle])) with the same type (ignoring cv-qualification) as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function's return value

由于 p.first 不是对象的名称,因此禁止RVO。

Since p.first is not the name of an object, RVO is prohibited.

这篇关于RVO是否在对象成员上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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