默认的移动构造函数采用const参数 [英] Default move constructor taking a const parameter
问题描述
定义类时,以下内容有效吗?
When defining a class, is the following valid?
T(const T&&) = default;
我在此处中了解了移动构造器,并且它解释了默认构造器仍可以隐式声明:
I was reading about move constructors here and it explains how the default may still be implicitly declared:
一个类可以有多个move构造函数,例如两者
T::T(const T&&)
和T::T(T&&)
.如果存在一些用户定义的move构造函数,则 用户仍然可以强制生成隐式声明的动作 关键字为default的构造函数.
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:
At the bottom of the page it mentions defect report CWG 2171:
CWG 2171 C ++ 14
X(const X&&) = default
不平凡,变得平凡.
CWG 2171 C++14
X(const X&&) = default
was non-trivial, made trivial.
也许Wiki条目只是有一个错误,并且 CWG 2171 仅指复制构造函数,而不是move构造函数?
Maybe the wiki entry just has a mistake and CWG 2171 is only referring to a copy constructor, not a move constructor?
推荐答案
摘自n4296草案:
8.4.2.1显式默认的函数:
明确默认的功能应
A function that is explicitly defaulted shall
(1.1)—是一个特殊的成员函数,
(1.1) — be a special member function,
(1.2)-具有相同的声明函数类型(可能的除外 不同的ref限定词,但在复制的情况下除外 构造函数或复制赋值运算符,参数类型可以是 对非常量T的引用",其中T是成员的名称 函数的类),就好像它已经隐式声明一样,并且
(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)-没有默认参数.
(1.3) — not have default arguments.
12.8.10复制和移动类对象:
隐式声明的类X的move构造函数将具有以下形式: X :: X(X&)
The implicitly-declared move constructor for class X will have the form X::X(X&&)
因此,该行:
As a consequence the line:
T(const T&&) = default;
无效,因为隐式声明的move构造函数的形式为:
is not valid because the implicitly-declared move constructor has the form of:
T(T&&)
这篇关于默认的移动构造函数采用const参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!