Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

c++ - Move constructors and the Strong Exception Guarantee

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...