移动构造函数和强异常保证 [英] Move constructors and the Strong Exception Guarantee

查看:143
本文介绍了移动构造函数和强异常保证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个快速的问题,我找不到一个很好的参考,特别是关于未来C ++ 0x标准的当前实现。



由于移动构造函数可以抛出,这意味着某些标准库函数不能提供强大的异常保证(例如 vector< T> :: resize())。



有一个建议是:1)使所有标准库移动构造函数不抛出,以及2)添加用户代码的编译时检查,以确保例如。

p>该提案发生了什么(尤其是关于此问题) ?最终草案中的问题如何解决?



最重要的是,当我使用最近的GCC或MSVC 10时,这对我来说意味着什么?标准库的这些实现是否提供了例如强异常保证。 std :: vector< MyTypeWithAThrowingMoveConstructor> :: resize()



编辑:我没有看到明显相关的这个问题。如果我的问题是重复的事实达成一致,那么关闭它。但是,我真正对实现感兴趣,而不是

解决方案

我没有检查具体的实现,但一般的想法是,如果移动构造函数可以抛出,则向量将必须复制元素。这样就有可能在异常情况下回滚。



甚至还有一个帮助函数 move_if_noexcept < utility> 中帮助它决定要做什么。


Just a quick question, on which I cannot find a good reference, especially with regard to current implementations of the future C++0x standard.

Since move constructors can throw, it means that some standard library functions cannot provide the strong exception guarantee (eg. vector<T>::resize()).

There was a proposal to 1) make all standard library move constructors "no throw", and 2) to add compile time checks on user code to ensure that eg. std::pair<std::string, MyType> defines a nothrow move constructor or no move constructor at all.

What happened to this proposal (esp. with respect to this question) ? How is the issue "solved" in the final draft ?

Most importantly, what does it imply for me when I use a recent GCC or MSVC 10 ? Does those implementations of the standard library provide the Strong Exception Guarantee on eg. std::vector<MyTypeWithAThrowingMoveConstructor>::resize() ?

EDIT: I didn't see this question which is clearly related. If there is a consensus on the fact that my question is a duplicate, then close it. However, I am really interested on what is implemented, not what has been discussed.

解决方案

I haven't checked the specific implementations, but the general idea is that if the move constructor can throw, the vector will have to copy the elements instead. That way it is possible to roll back in case of an exception.

There is even a helper function move_if_noexcept defined in <utility> to help it decide what to do.

这篇关于移动构造函数和强异常保证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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