移动构造函数是否需要为noexcept? [英] Are move constructors required to be noexcept?
问题描述
我一直在阅读一些矛盾的文章,关于移动构造函数/赋值是否允许抛出。
I've been reading some contradicting articles in regards whether move constructors/assignment is allowed to throw or not.
因此,我想问是否move constructors / assignment允许抛出最后的C ++ 11标准?
Therefore I'd like to ask whether move constructors/assignments are allowed to throw in the final C++11 standard?
推荐答案
是。 应该吗?不是。
一般来说,你在里面做的任何事情都不应该是可能的。你不应该分配内存,调用其他代码或类似的东西。写一个移动构造函数的唯一原因是离开与别人的内存指针和对象引用。您应该复制几个基本类型并将其他对象中的值置零。那些东西不应该抛出。
In general, nothing you do within them should be anything that could throw. You shouldn't be allocating memory, calling other code, or anything like that. The only reason to write a move constructor is to abscond with someone else's memory pointers and object references. You should be copying a few basic types and nulling out the values in the other object. Those things shouldn't throw.
因此,虽然允许,这不是一个好主意。如果你这样做,重新思考你在你的移动操作中做什么。
So while it is allowed, it's not a good idea. If you're doing it, rethink what you're doing in your move operations.
这篇关于移动构造函数是否需要为noexcept?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!