C ++移动语义和异常 [英] C++ Move semantics and Exceptions

查看:118
本文介绍了C ++移动语义和异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在即将到来的C ++ 0x标准中,当在移动构造函数内/中抛出异常时会发生什么?

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 original and move-to object in an undefined state? what are the guarantees afforded by the language?

推荐答案

我认为标准委员会最初试图使它移动构造函数不会

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.

提案N3050,允许移动构造函数抛出(允许抛出异常) Rev 1),已被纳入标准草案。本质上,该建议增加了移动构造函数抛出的能力,但是不允许抛出移动以用于需要强异常安全性保证的某些操作(如果非抛出移动不需要,则库将回退到复制对象) t可用)。

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).

如果将移动构造函数标记为非抛出( noexcept )并抛出异常,std ::将调用terminate()。

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

这也许值得阅读David Abrahams的博客文章,讨论N3050打算解决的问题:

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/

这篇关于C ++移动语义和异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆