C++ Move semantics and Exceptions

后端 未结 3 989
长发绾君心
长发绾君心 2020-12-03 01:06

In the forthcoming C++0x standard, what happens when an exception is thrown within/during the move constructor?

Will the original object remain? or are both the orig

3条回答
  •  感动是毒
    2020-12-03 02:00

    I believe that the standards committee originally attempted to make it so move constructors would not be allowed to throw exceptions, but (at least as of today) found that trying to enforce that had too many pitfalls.

    Proposal N3050, "Allowing Move Constructors to Throw (Rev 1)", has been incorporated into the draft standard. Essentially the proposal adds the capability for move constructors to throw, but to disallow 'throwing' moves to be used for certain operations where strong exception safety guarantees were needed (the library will fall back to copying the object if a non-throwing move isn't available).

    If you mark a move constructor as non-throwing (noexcept) and an exception is thrown, std::terminate() will be called.

    • http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3050.html

    It might also be worth reading a blog article by David Abrahams that discusses the issues that N3050 was intended to address:

    • http://cpp-next.com/archive/2009/10/exceptionally-moving/

提交回复
热议问题