简体   繁体   English

前进或移动

[英]Forward or Move

Are these valid usage of move and forward? 这些是前进和前进的有效用法吗?
Are f3 and f4 the same? f3和f4相同吗?
Is it dangerous to do so? 这样做有危险吗?
Thank you! 谢谢!

#include <utility>
class A {};
A f1() {
  A a;
  return a;   // Move constructor is called
}
A f2(A&& a) {
  return a;   // Copy constructor is called, which is what I try to avoid.
}
A f3(A&& a) {
  return std::forward<A&&>(a); // Move constructor is called
}
A f4(A&& a) {
  return std::move(a); // Move constructor is called
}
  • Use std::forward with a universal reference , ie a template <typename T> ... T&& . std::forward通用引用一起使用 ,即template <typename T> ... T&&

  • Use std::move with an rvalue reference (like your A&& ). 使用带有右值引用的 std::move (例如A&& )。

So both f1 and f4 are plausible solutions. 因此, f1f4都是合理的解决方案。 They do different things, so you have to decide which one you want. 他们做不同的事情,因此您必须决定要选择哪一个。

Do not use f2 or f3 . 不要使用f2f3

std::forward exists because of a quirk in how && works under type deduction. std::forward之所以存在是因为&&在类型推导下的工作方式存在怪异。

Under type deduction, the T in T&& will bind to one of 3 possibilities. 下型扣, TT&&将结合的3种可能性之一。 If being deduced from an lvalue int& , T will bind to int& . 如果从左值int&推导,则T将绑定到int& Then int& && is just a int& . 那么int& &&只是int& If being deduced from an lvalue int const& , T will bind to int const& , and int const& && is int const& . 如果从左值int const&推导,则T将绑定到int const& ,并且int const& &&int const& If being deduced from an rvalue int of some kind, T will bind to int , and int&& is int&& . 如果从某种右值int推导,则T将绑定到int ,并且int&&int&&

std::forward is a utility function to reverse that map. std::forward是一个实用函数,用于反转该映射。 The three pertinent signatures of std::forward<> are: T& std::forward<T&>(T&) or T const& std::forward<T const&>(T const&) or T&& std::forward<T>(T&&) std::forward<>的三个相关签名为: T& std::forward<T&>(T&)T const& std::forward<T const&>(T const&)T&& std::forward<T>(T&&)

All of this ends up being exceedingly useful when doing the technique known as "perfect forwarding", where you use T&&t in a type deduction context, then std::forward<T>(t) to pass on the "same type" as was deduced from to another call. 当进行称为“完美转发”的技术时,所有这些最终都非常有用,在这种技术中,您在类型推导上下文中使用T&&t ,然后std::forward<T>(t)传递与以前相同的“相同类型”推到另一个电话。

Note that there are a few simplifying lies above. 请注意,上面有一些简化之处。 There are is also the possibility of T const&& which is pretty obscure type-wise, as an example. 例如, T const&&的可能性在类型上也相当模糊。 I probably glossed over some details of how the type deduction works, and the terms rvalue and lvalue don't fully reflect the full 5-fold (or is it 6?) different kinds of variable values in C++11. 我可能掩盖了类型推导的工作原理的一些细节,术语rvaluelvalue不能完全反映C ++ 11中不同类型变量值的全5倍(或者是6?)。

For your example, they will do the same thing, but it is idiomatic to use std::move 对于您的示例,它们将执行相同的操作,但是使用std::move是惯用的

A f(A&& a) {
  // use std::move(a)
}

A slightly different case is with function templates 功能模板的情况略有不同

template<typename A>
A f(A&& a) {
   // use std::forward<A>(a)
}

The difference is that the second version can receive both lvalues and rvalues (Scott Meyers named them " universal references "), whereas the first version can only receive rvalues. 不同之处在于,第二个版本可以同时接收左值和右值(Scott Meyers将其命名为“ 通用引用 ”),而第一个版本只能接收右值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM