我何时应无例外地声明move构造函数? [英] When should I declare a move constructor without noexcept?

查看:50
本文介绍了我何时应无例外地声明move构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该标准不对移动构造函数强制执行noexcept.在什么情况下,构造器抛出异常是可以接受的/必要的?

The standard doesn't enforce noexcept on move constructors. In what circumstances is it acceptable/neccesary for a move constructor to throw?

推荐答案

当您真的别无选择时.大多数情况下,您的move构造函数应为 noexcept .而且它们是默认设置.

When you really have no choice. Most of the time your move constructor should be noexcept. And they are by default.

请参阅以下内容: http://www.codingstandard.com/rule/12-5-4-declare-noexcept-the-move-constructor-and-move-assignment-operator/

对于类型为no的其他类型使用noexcept尤其重要旨在与标准库容器一起使用.如果搬家容器中元素类型的构造函数不是容器将使用复制构造函数而不是移动构造函数.

It is especially important to use noexcept for types that are intended to be used with the standard library containers. If the move constructor for an element type in a container is not noexcept then the container will use the copy constructor rather than the move constructor.

这篇关于我何时应无例外地声明move构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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