6

The specification for vector's move constructor is (copied out of the standard):

vector(vector&&); 

Notice the lack of noexcept. But both gcc 4.8 and Clang 3.2 report that std::is_nothrow_move_constructible<std::vector<int>>::value returns true (i.e, 1):

#include<vector> #include<iostream> int main() { std::cout << std::is_nothrow_move_constructible<std::vector<int>>::value << '\n'; } 

What is the cause of this apparent discrepancy?

0

1 Answer 1

7

The standard allows an implementation to strengthen the exception specification of a method as per

17.6.5.12 Restrictions on exception handling [res.on.exception.handling]

4 Destructor operations defined in the C++ standard library shall not throw exceptions. Every destructor in the C++ standard library shall behave as if it had a non-throwing exception specification. Any other functions defined in the C++ standard library that do not have an exception-specification may throw implementation-defined exceptions unless otherwise specified.191 An implementation may strengthen this implicit exception-specification by adding an explicit one.192

Sign up to request clarification or add additional context in comments.

4 Comments

In the case of gcc 4.8, I see that its vector move constructor is declared noexcept.
+1 And this strengthening can have a significant (positive) performance impact on vector<vector<int>>::push_back. Said differently, the lack of the noexcept creates a significant performance penalty for cases like this.
Different compilers seem to be very different. Gcc 4.8 reports (via std::is_nothrow_move_constructible) that vector, list, string, set, multiset, map, and multimap are noexcpt. Clang 3.2 reports that all standard containers are noexcept. VS2013 reports that only string is noexcept.
@KnowItAllWannabe In the end it's an implementation detail, but as Howard mentioned it's quite important as it unlocks further optimization opportunities. It seems both GCC and Clang have done their homework :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.