C ++ 11: do objects return by value never throw exceptions if ctor motion is defined?

In C ++ 11 and later standards, is it guaranteed that a (possibly throwing exception) copy of ctor is not called when a class object returns by value from a function - provided that ctor movement is defined for this class? Prerequisites: 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, for example, an expression such as

some_other_func(my_func(t));

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

+4
source share
2 answers

: , , , . , . , , noexcept.

( ) : noexcept , , , noexcept.

+4

++, .

, , , , , (), , t .

noexcept, , ( ). , , ( , , ).

, X(X&&) X() noexcept, .

, , some_other_function X, , X, , some_other_function .

+1

Source: https://habr.com/ru/post/1687360/


All Articles