定义类时,以下是否有效?
T(const T&&) = default;
我正在阅读移动构造函数 here并且它解释了如何仍然可以隐式声明默认值:
A class can have multiple move constructors, e.g. both
T::T(const T&&)
andT::T(T&&)
. If some user-defined move constructors are present, the user may still force the generation of the implicitly declared move constructor with the keyword default.
在页面底部,它提到了缺陷报告 CWG 2171:
CWG 2171 C++14
X(const X&&) = default
was non-trivial, made trivial.
也许 wiki 条目有一个错误 CWG 2171仅指复制构造函数,而不是移动构造函数?
最佳答案
来自 n4296 草案:
8.4.2.1 显式默认函数:
A function that is explicitly defaulted shall
(1.1) — be a special member function,
(1.2) — have the same declared function type (except for possibly differing ref-qualifiers and except that in the case of a copy constructor or copy assignment operator, the parameter type may be “reference to non-const T”, where T is the name of the member function’s class) as if it had been implicitly declared, and
(1.3) — not have default arguments.
12.8.10 复制和移动类对象:
The implicitly-declared move constructor for class X will have the form X::X(X&&)
结果是:
T(const T&&) = default;
无效,因为隐式声明的移动构造函数具有以下形式:
T(T&&)
关于c++ - 采用 const 参数的默认移动构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41551545/