移动赋值运算符的异常说明符如何影响move constructor的异常说明符? [英] How could the exception specifier on move assignment operator affect that of move constructor?

查看:115
本文介绍了移动赋值运算符的异常说明符如何影响move constructor的异常说明符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++ 14模式下使用GCC 5.2和clang 3.6进行测试,并且它们提供相同的输出。

I've being testing with GCC 5.2 and clang 3.6, both in C++14 mode, and they give the same output.

对于以下代码

#include <iostream>
#include <type_traits>

struct S {
  // S& operator= (S&&) noexcept { return *this; }
};


int main() {
  std::cout << std::is_nothrow_move_constructible<S>::value
            << std::is_nothrow_move_assignable<S>::value;  
}

结果 11 。但如果取消注释移动赋值运算符,输出将变为 01 。如何在移动赋值运算符上明确的 noexcept 指定可能影响移动构造函数的指定?

the result 11 is obtained. But if uncomment the move assignment operator, the output becomes 01. How could an explicit noexcept specification on the move assignment operator possibly affect that of the move constructor?

推荐答案

通过定义移动赋值运算符,您可以禁用移动构造函数,因为规则5 。该类不是 is_nothrow_move_constructible ,因为它不是可移动可构造的,该构造函数不再可用,除非您定义它。

By defining the move assignment operator, you disabled the move constructor due to the rule of 5. The class isn't is_nothrow_move_constructible because it isn't move constructable at all, that constructor is no longer available unless you define it.

§12.8复制和移动类对象


如果类 X 没有显式声明一个移动构造函数,如果且仅当

- X 没有用户声明的复制构造函数,

- X 没有用户声明的复制赋值运算符,

- X 没有用户声明的移动分配运算符

- X 没有用户声明的析构函数,

- 移动构造函数不会被隐式定义为已删除。

If the definition of a class X does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if
X does not have a user-declared copy constructor,
X does not have a user-declared copy assignment operator,
X does not have a user-declared move assignment operator,
X does not have a user-declared destructor, and
— the move constructor would not be implicitly defined as deleted.

在没有用户定义的move构造函数的情况下,两者都被隐式定义并遵循以下规范。

In the case where you had no user-defined move constructor, both were implicitly defined and followed the below specification.

§15.4异常规范


隐式声明的特殊成员函数应具有异常规范。如果 f 是隐式声明的默认构造函数,复制构造函数,移动构造函数,析构函数,复制赋值操作符或移动赋值运算符,其隐式异常规范指定type-id T 如果且仅当 T f 的隐含定义; f 应允许所有异常,如果它直接调用的任何函数允许所有异常,并且 f 如果每个函数直接调用允许没有异常

An implicitly declared special member function shall have an exception-specification. If f is 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 shall allow all exceptions if any function it directly invokes allows all exceptions, and f shall allow no exceptions if every function it directly invokes allows no exceptions.

这篇关于移动赋值运算符的异常说明符如何影响move constructor的异常说明符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆