我何时应无例外地声明move构造函数? [英] When should I declare a move constructor without noexcept?
问题描述
该标准不对移动构造函数强制执行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.
对于类型为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屋!