gpt4 book ai didi

括号中带有返回语句的 C++ nrvo/copy elision

转载 作者:太空狗 更新时间:2023-10-29 20:09:10 24 4
gpt4 key购买 nike

我在胡乱使用以下代码,使用我的 visual studio 2017 应用程序和两个不同的在线编译器得到了不同的结果。在 Release模式下,visual studio 确实在两种情况下都省略了复制/移动,而两个在线编译器只是在没有括号的 return 语句的情况下这样做。我的问题是:谁是对的,更重要的是底层规则是什么。 (我知道您可以将括号与 decltype(auto) 语法结合使用。但这不是当前用例)。

示例代码:

#include <iostream>
#include <cstdio>

struct Foo
{
Foo() { std::cout << "default constructor" << std::endl; }
Foo(const Foo& rhs) { std::cout << "copy constructor" << std::endl; }
Foo(Foo&& rhs) { std::cout << "move constructor" << std::endl; }
Foo& operator=(const Foo& rhs) { std::cout << "copy assignment" << std::endl; return *this; }
Foo& operator=(Foo&& rhs) { std::cout << "move assignment" << std::endl; return *this; }
};

Foo foo_normal()
{
Foo a{};
return a;
}

Foo foo_parentheses()
{
Foo a{};
return (a);
}

int main()
{
auto a = foo_normal();
auto b = foo_parentheses();
std::getchar();
}

在线编译器1: http://cpp.sh/75bux

在线编译器2: http://coliru.stacked-crooked.com/a/c266852b9e1712f3

visual studio 在 Release模式下的输出是:

default constructor
default constructor

在其他两个编译器中,输出是:

default constructor
default constructor
move constructor

最佳答案

这是 the relevant quote from the standard :

This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):

(1.1) - 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 call's return object

所以要求是

  1. 在返回声明中
  2. 在函数中
  3. 具有类返回类型
  4. 当表达式是非 volatile 自动对象的名称时(函数参数或由处理程序的异常声明引入的变量([except.handle])除外)
  5. 具有与函数返回类型相同的类型(忽略 cv 限定)

我认为满足了要求 1、2、3 和 5,但没有满足要求 4。 (a) 不是对象的名称。因此对于给定的代码复制省略不适用。由于移动构造函数有副作用,因此在 as-if 规则下也不能将其省略。

因此 gcc 是正确的,visual studio(和 clang)在这里是错误的。

关于括号中带有返回语句的 C++ nrvo/copy elision,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48749440/

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