c++ - 采用 const 参数的默认移动构造函数

标签 c++ c++11 constructor language-lawyer

定义类时,以下是否有效?

T(const T&&) = default;

我正在阅读移动构造函数 here并且它解释了如何仍然可以隐式声明默认值:

A class can have multiple move constructors, e.g. both T::T(const T&&) and T::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/

相关文章:

c++ - 架构 x86_64 的 undefined symbol

C++11 static_assert : Parameterized error messages

c++ - 使用 Clang 构建 C++ 静态库时的奇怪行为

c++ - 模板元编程如何专注于集合

c++ - 可以创建任意类型的 constexpr 链表吗?

c++ - "No-Const Pointer to Const "调用函数

c++ - std::notify_all_at_thread_exit 是如何工作的?

java - mockito如何创建模拟对象的实例

class - Scala.js 在全局范围内看不到 JS *class*,但看到构造函数

language-agnostic - 初始化或构造