问题描述
[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类型也可以有移动构造函数。它只是做一个副本,但它仍然称为移动构造函数。
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&&&&;)`称为移动构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!