473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Exception guarantees for STL container constructors

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?
Jul 22 '05 #1
1 2368
In article <78**************************@posting.google.com >,
sh***@shaneandsusan.com (Shane Groff) wrote:
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
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Johan Bergman | last post by:
Hi, Maybe someone can help me with this one. The following describes a somewhat simplified version of my problem, but I think it will be sufficient. I want to use class factories (virtual...
7
by: Douglas Peterson | last post by:
Take a look at this code, it looks funny as its written to be as short as possible: -- code -- struct Base { ~Base() { *((char*)0) = 0; } }; struct Derived : public Base
0
by: Joe | last post by:
Reposting here as there were no useful replies in the dotnet.framework NG... What is the correct pattern for handling exceptions in IDisposable.Dispose, especially in a class that manages...
1
by: Daniel Klein | last post by:
When creating a custom exception that derives from ApplicationException, why is it necessary to have the 3 basic contructors, i.e. Public Sub New() Public Sub New(ByVal message As String)...
1
by: yancheng.cheok | last post by:
Hi all, According to "How can I handle a constructor that fails?" in http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.2, whenever there is a constructor fail, we will throw...
3
by: matko | last post by:
This is a long one, so I'll summarize: 1. What are your opinions on raising an exception within the constructor of a (custom) exception? 2. How do -you- validate arguments in your own...
7
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
11
by: mathieu | last post by:
Hi there, I'd like to know if there is a general answer to the following question: when making a public interface that access a container should the function throw an exception when the element...
12
by: raj s | last post by:
What is the advantage and disadvantage of using function declaration with exceptions specified. like foo()throws(char&)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.