jeudi 12 octobre 2017

C++11: does returning objects by value never throw exceptions when a move ctor is defined?

In C++11 and later standards, is it guaranteed that the (possibly exception-throwing) copy ctor is not called when returning a class object by value from a function - provided a move ctor is defined for this class? Background: Suppose

struct X {
   X() {}
   X(const X&) {/* code that might throw exceptions */}
   X(X&&) {/* code that never throws exceptions */}
   ...
};

and

X my_func(some_type& t)
{
   X x;
   // code that modifies t and x but never throws exceptions
   return x;
}

Now, does, for instance, an expression such as

some_other_func(my_func(t));

never throw exceptions (i.e., is this guaranteed?) provided the function some_other_func(const X&) does not throw exceptions? And what if the signature of some_other_func were some_other_func(X)?

Aucun commentaire:

Enregistrer un commentaire