Exception guarantees for STL container constructors

S

Shane Groff

I'm sorry if this is a FAQ, but I've been unable to find exactly what
I'm looking for.

Does the STL make the no-throw guarantee for any of the STL
containers?

E.g. If I have a std::vector<int> on the stack, using my current STL
implementation, as far as I can see, there is no chance for an
exception to occur, because the default constructor for a vector does
not need to do any extra allocations.
On the other hand a std::list<int> will allocate a head node, so it
may throw (obviously if the container is allocated from the free
store, the call to 'new' may throw, but I'm concerned about the
behavior when the container is on the stack).

Is this behavior determined by the standard, and if so, where can I
find out which constructors will not throw?
As a generalization, is there an on-line reference that lists what
exceptions, if any, each of the container member functions are allowed
to throw?
 
H

Howard Hinnant

I'm sorry if this is a FAQ, but I've been unable to find exactly what
I'm looking for.

Does the STL make the no-throw guarantee for any of the STL
containers?

E.g. If I have a std::vector<int> on the stack, using my current STL
implementation, as far as I can see, there is no chance for an
exception to occur, because the default constructor for a vector does
not need to do any extra allocations.
On the other hand a std::list<int> will allocate a head node, so it
may throw (obviously if the container is allocated from the free
store, the call to 'new' may throw, but I'm concerned about the
behavior when the container is on the stack).

Is this behavior determined by the standard, and if so, where can I
find out which constructors will not throw?
As a generalization, is there an on-line reference that lists what
exceptions, if any, each of the container member functions are allowed
to throw?

The standard declares none of the container default constructors to be
nothrow.

The Metrowerks implementation (Pro 9) of the std::containers provides
the nothrow guarantee for all of the default constructors, even for
std::list<T> (assuming any client-supplied allocator default constructor
doesn't throw). Your vendor (should it not be Metrowerks) may or may
not provide a similar guarantee for none, some, or all of the
std::containers.

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top