gpt4 book ai didi

c++ - RVO 是否适用于对象成员?

转载 作者:IT老高 更新时间:2023-10-28 23:13:05 30 4
gpt4 key购买 nike

考虑以下几点:

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

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

auto a = foo();

p.first 会被复制、移动或 RVO 编辑吗?

最佳答案

我发现在 Visual Studio 2010 和 gcc-5.1 中,RVO 应用(参见示例 http://coliru.stacked-crooked.com/a/17666dd9e532da76)。

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

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。

关于c++ - RVO 是否适用于对象成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33344259/

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