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
get_pair的结果是移动构建的,正如预期的那样. NRVO也可能完全消除了这一举动,但现在不在于这个问题.
get_tuple_moved的结果也是移动构造的,它被明确指定为这样.然而,get_tuple的结果是复制构造的,这对我来说是完全不明显的.
我认为传递给return语句的任何表达式都可以被认为是对它有隐含的动作,因为编译器知道它即将超出范围.好像我错了有人可以澄清,这里发生了什么?
另请参见相关但不同的问题:When should std::move be used on a function return value?
解决方法
相关的措辞可以在[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().
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。