0

I'm reading some libraries for my project. Most of them specify "noexcept" in any move constructor. Is it necessary to write "noexcept" in move constructors or is it occasionally just happened in my reading codes?

Thank you.

mallea
  • 534
  • 6
  • 17

1 Answers1

-1

I think this explains the reason why you see noexcept in move constructors:

"A class provides the Strong Exception Guarantee if after an exception occurs, the objects maintain their original values. The move members of a class explicitly change the state of their argument. Should an exception be thrown after some members have been moved, then the Strong Exception Guarantee may no longer hold as the from object has been modified. 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."

So to answer your question, you should use noexcept in every case where your constructor could throw an exception. If you don't use one and if an exception occurs, the Strong Exception Guarantee will fail which could cause problems in the error recovery of your code.

Source: http://www.codingstandard.com/rule/12-5-4-declare-noexcept-the-move-constructor-and-move-assignment-operator/

Dave S
  • 973
  • 9
  • 17