Ralph Tandetzky
Ralph Tandetzky

Reputation: 23640

Is std::vector exception neutral?

Exception for the member function at(), I cannot think of exceptions being thrown by std::vector functions but by its allocator or by its elements constructors and assignment operators. Therefore, exception neutrality can be a very desirable guarantee when implementing your own allocators. For example, you might allocate all required memory up-front and then rely on the noexcept guarantee of an allocator to propagate to its container.

Does the C++ standard provide exception neutrality of std::vector<T,Alloc>?

Upvotes: 2

Views: 1112

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118445

Besides memory allocations, there is one situation where vector itself will also throw exceptions: the at() member function.

So, after reserve()ing sufficient memory, you are guaranteed that no exceptions will be thrown, provided that the vector's class's constructors and assignment operators (which will be called as part of modifying the vector) do not throw any exceptions either, and there are no out of range calls to at().

Upvotes: 1

Related Questions