std::queue constructor throws - what else?

C

cppsks

We recently figured out that std::queue's constructor throws. I believe it
calls a method on std::deque (the default data structure it uses for the
queue) and that method threw. The method had to do with initialization.

Anyway, the std::queue was allocated on the "stack" - it was an automatic
variable:
std::queue<int> intQ;

Well, I have no problems with queue's constructor throwing but the problem
is that at work, none of the STL containers that are allocated on the stack
have a try/catch block around them. Of course, the insert or push_back
methods have try/catchs around them.

So the general question is: What STL containers throw in their constructor?
It almost seems like it is implementation dependent, in which case how would
you approach a problem such as this?

Thanks,
sks
 
T

Tom Widmer

We recently figured out that std::queue's constructor throws. I believe it
calls a method on std::deque (the default data structure it uses for the
queue) and that method threw. The method had to do with initialization.

Anyway, the std::queue was allocated on the "stack" - it was an automatic
variable:
std::queue<int> intQ;

Well, I have no problems with queue's constructor throwing but the problem
is that at work, none of the STL containers that are allocated on the stack
have a try/catch block around them. Of course, the insert or push_back
methods have try/catchs around them.

So the general question is: What STL containers throw in their constructor?

All of them, although usually only the node based containers are
likely to. I'm quite surprised that the deque default constructor is
throwing for you - it certainly doesn't with Dinkumware's library.
It almost seems like it is implementation dependent, in which case how would
you approach a problem such as this?

I find it unlikely that you can handle a bad_alloc at the site of
container creation, so just let the exception propogate up the stack
to an appropriate place (such as the place the attempted "operation"
was started from). You are using RAII, right?

Tom
 
P

Peter Koch Larsen

cppsks said:
We recently figured out that std::queue's constructor throws. I believe it
calls a method on std::deque (the default data structure it uses for the
queue) and that method threw. The method had to do with initialization.

Anyway, the std::queue was allocated on the "stack" - it was an automatic
variable:
std::queue<int> intQ;

Well, I have no problems with queue's constructor throwing but the problem
is that at work, none of the STL containers that are allocated on the
stack
have a try/catch block around them. Of course, the insert or push_back
methods have try/catchs around them.

Why? I very much doubt that this is the correct design. Can you always
handle the failure of a push_back?
So the general question is: What STL containers throw in their
constructor?
It almost seems like it is implementation dependent, in which case how
would
you approach a problem such as this?
They could all throw - in case of no available memory.
Thanks,
sks
/Peter
 

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
474,184
Messages
2,570,976
Members
47,536
Latest member
MistyLough

Latest Threads

Top