加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 返回兼容类型时为什么需要显式的std :: move?

发布时间:2020-12-16 05:45:41 所属栏目:百科 来源:网络整理
导读:我正在看STL的“ Don’t Help the Compiler”演讲,他在幻灯片26上有一个类似的例子: struct A{ A() = default; A(const A) { std::cout "copied" std::endl; } A(A) { std::cout "moved" std::endl; }};std::pairA,A get_pair(){ std::pairA,A p; return p;
我正在看STL的“ Don’t Help the Compiler”演讲,他在幻灯片26上有一个类似的例子:
struct A
{
  A() = default;
  A(const A&) { std::cout << "copied" << std::endl; }
  A(A&&) { std::cout << "moved" << std::endl; }
};

std::pair<A,A> get_pair()
{
  std::pair<A,A> p;
  return p;
}

std::tuple<A,A> get_tuple()
{
  std::pair<A,A> get_tuple_moved()
{
  std::pair<A,A> p;
  return std::move(p);
}

有了这个,以下电话:

get_pair();
get_tuple();
get_tuple_moved();

产生此输出:

moved
moved
copied
copied
moved
moved

See MCVE in action.

get_pair的结果是移动构建的,正如预期的那样. NRVO也可能完全消除了这一举动,但现在不在于这个问题.

get_tuple_moved的结果也是移动构造的,它被明确指定为这样.然而,get_tuple的结果是复制构造的,这对我来说是完全不明显的.

我认为传递给return语句的任何表达式都可以被认为是对它有隐含的动作,因为编译器知道它即将超出范围.好像我错了有人可以澄清,这里发生了什么?

另请参见相关但不同的问题:When should std::move be used on a function return value?

解决方法

get_tuple()中的return语句应该使用move-constructor进行复制初始化,但由于返回表达式的类型和返回类型不匹配,所以选择复制构造函数.在C14中有一个变化,现在有一个初始阶段的重载分辨率,它将return语句当作一个rvalue,当它只是在body中声明的一个自动变量时.

相关的措辞可以在[class.copy] / p32中找到:

When the criteria for elision of a copy/move operation are met,[..],or when the expression in a return statement is a (possibly parenthesized) id-expression that names an object with automatic storage duration declared in the body [..],overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue.

所以在C14中,所有的输出都应该来自A的移动构造器.

clang和gcc的中继版本已经实现了这一变化.要在C 11模式下获得相同的行为,您需要在return语句中使用一个显式的std :: move().

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读