为什么`T(const T&&&&;)`称为移动构造函数? [英] Why is `T(const T&&)` called a move constructor?

查看:203
本文介绍了为什么`T(const T&&&&;)`称为移动构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[C ++ 11:12.8 / 3]: code>是一个move构造函数,如果它的第一个参数是 typeX&&& const X&&< / code >, volatile X&&& const volatile X&&< / code>,并且没有其他参数否则所有其他参数都有默认参数(8.3.6)。 [..]


为什么是一个 const rvalue引用称为移动构造函数的标准?当然,是不言而喻的这个禁止有意义的移动语义,除了大多数fringey案件?



根据我,正如SO说,由于 ,所以不应将T(const T&&)视为移动构造函数



如果有任何

解决方案

这里有一些移动构造函数和其他构造函数之间的区别:




  • 移动构造函数可以是默认的

  • 移动构造函数不会阻止类型成为

  • 移动构造函数阻止隐式移动构造函数的生成


  • 移动构造函数可以通过标准库函数自动调用



对于所有这些,不调用 X(const X&&)一个移动构造函数会产生不良结果。



你给一个替代:它可能被称为复制构造函数。这似乎没有不良的结果:它会抑制隐式复制构造函数。



移动构造函数是否移动实际上并不重要。 POD类型也可以有移动构造函数。它只是做一个副本,但它仍然称为移动构造函数。


[C++11: 12.8/3]: A non-template constructor for class X is a move constructor if its first parameter is of typeX&&, const X&&, volatile X&&, or const volatile X&&, and either there are no other parameters or else all other parameters have default arguments (8.3.6). [..]

Why is a constructor that takes a const rvalue reference called a "move constructor" by the standard? Surely it's self-evident that this prohibits meaningful move semantics in all but the most fringey cases?

"According to me", as the SO saying goes, T(const T&&) shouldn't be deemed a "move constructor" as such, since it's basically useless.

If anything, shouldn't it be called a copy constructor?

解决方案

Here are some of the differences between move constructors and other constructors:

  • Move constructors can be defaulted
  • Move constructors don't prevent a type from being a "literal type"
  • Non-trivial move constructors prevent a type from being a "trivially copyable type"
  • Move constructors prevent the implicit move constructor from being generated
  • Move constructors may be automatically called by standard library functions

As far as I can tell, for all of those, not calling X(const X &&) a move constructor gives undesirable results.

You give an alternative: it might be called a copy constructor instead. That too seems to have undesirable results: it would suppress the implicit copy constructor.

Whether a move constructor actually moves doesn't matter. A POD type may have a move constructor too. It'll just be making a copy, but it's still called a move constructor.

这篇关于为什么`T(const T&amp;&amp;&amp;&amp;;)`称为移动构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆