默认的 Move 构造函数是否定义为 noexcept?

2021-12-30 00:00:00 constructor c++ c++11 move-semantics

在重新分配时决定是移动还是复制元素之前,向量似乎会检查移动构造函数是否标记为 noexcept .默认移动构造函数是否定义为 noexcept?我看到了以下文档,但没有说明这一点.http://en.cppreference.com/w/cpp/language/move_constructor

It seems that a vector will check if the move constructor is labeled as noexcept before deciding on whether to move or copy elements when reallocating. Is the default move constructor defined as noexcept? I saw the following documentation but it didn't specify this.http://en.cppreference.com/w/cpp/language/move_constructor

隐式声明的移动构造函数

Implicitly-declared move constructor

如果没有用户定义的移动为类类型(结构、类或联合)提供构造函数,并且以下所有情况都为真:没有用户声明的副本构造函数没有用户声明的复制赋值运算符没有用户声明的移动赋值运算符用户声明的析构函数隐式声明的移动构造函数是由于下一节中详述的条件,未定义为已删除然后编译器会将移动构造函数声明为内联公共具有签名 T::T(T&&) 的类的成员多个移动构造函数,例如T::T(const T&&) 和 T::T(T&&).如果存在一些用户定义的移动构造函数,用户可能仍然强制生成隐式声明的移动构造函数关键字默认.

If no user-defined move constructors are provided for a class type (struct, class, or union), and all of the following is true: there are no user-declared copy constructors there are no user-declared copy assignment operators there are no user-declared move assignment operators there are no user-declared destructors the implicitly-declared move constructor is not defined as deleted due to conditions detailed in the next section then the compiler will declare a move constructor as an inline public member of its class with the signature T::T(T&&) 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.

推荐答案

我认为答案是 15.4/14(异常规范):

I think the answer is 15.4/14 (Exception specifications):

继承构造函数 (12.9) 和隐式声明的特殊成员函数(第 12 条)具有异常规范.如果 f 是继承构造函数或隐式声明的默认构造函数、复制构造函数、移动构造函数、析构函数、复制赋值运算符或移动赋值运算符,则其隐式异常-specification 指定 type-id T 当且仅当 T 被 exception-specification 允许f 的隐式定义直接调用的函数;f 允许所有异常,如果它直接调用的任何函数允许所有异常,并且 f 具有 exception-specification noexcept(true) 如果它直接调用的每个函数都不允许例外.

An inheriting constructor (12.9) and an implicitly declared special member function (Clause 12) have an exception-specification. If f is an inheriting constructor or an implicitly declared default constructor, copy constructor, move constructor, destructor, copy assignment operator, or move assignment operator, its implicit exception-specification specifies the type-id T if and only if T is allowed by the exception-specification of a function directly invoked by f’s implicit definition; f allows all exceptions if any function it directly invokes allows all exceptions, and f has the exception-specification noexcept(true) if every function it directly invokes allows no exceptions.

基本上,它按你的想法行事,并且隐式声明的移动构造函数是 noexcept .

Basically, it Does What You Think, and the implicitly-declared move constructor is noexcept whenever it can be.

相关文章