I’ve been reading some contradicting articles in regards whether move constructors/assignment is allowed to throw or not.
Therefore I’d like to ask whether move constructors/assignments are allowed to throw in the final C++11 standard?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Are move constructors in general allowed to throw? Yes. Should they? No.
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.