exceptions and container modifiers

S

subramanian100in

In the ISO/IEC 14882:2003 document, in page 468 section '23.1
Container requirements', item 10 mentions the following:

"Unless otherwise specified (see 23.2.1.3 and 23.2.4.3) all container
types defined in this clause meet the following additional
requirements:
- if an exception is thrown by an insert() function while inserting
a single element, that function has no effects.
- if an exception is thrown by push_back() or push_front()
function, that function has no effects"

Then in page 480 section 23.2.1.3 - deque modifiers, item 2 says:
"Notes: if an exception is thrown other than by the copy constructor
or assignment operator of 'T', there are no effects."
(Here 'T' is the element type of the container).

My questions:
To me, there appears to be a difference between the statements in page
468 and page 480. However, since page 468 adds the words "unless
otherwise specified (see 23.2.1.3 and 23.2.4.3) ...", we have to
consider only the item 2 in page 480. Am I correct ?

Item 2 in page 480 section 23.2.1.3 - deque modifiers, says that "if
an exception is thrown other than by the copy constructor or
assignment operator of 'T', there are no effects." Given this, suppose
the copy ctor and assignment operator of 'T' do not throw any
exceptions, but due to non-availability of memory, suppose bad_alloc
exception is thrown by insert() or push_back() or push_front()
function. Then the original container on which these functions were
called, will be kept in tact(ie the container will not be modified at
all). Am I correct ?. Now suppose either the copy ctor or the
assignment operator of 'T' throws some exception. Then what will
happen to the container on which these functions were called ? Will it
be partially modified ?

For example, consider:
container.insert(iter, first, last);
When this function is called, while inserting the elements in the the
range [first, last) into 'container', suppose an exception is thrown
by either the copy ctor or the assignment operator of the element
type. Then will the 'container' maintain its original state before the
insert() was called or will there be some extra elements added to the
'container' prior to the exception thrown ?

Kindly explain.

Thanks
V.Subramanian
 
H

Howard Hinnant

In the ISO/IEC 14882:2003 document, in page 468 section '23.1
Container requirements', item 10 mentions the following:

"Unless otherwise specified (see 23.2.1.3 and 23.2.4.3) all container
types defined in this clause meet the following additional
requirements:
   - if an exception is thrown by an insert() function while inserting
a single element, that function has no effects.
   - if an exception is thrown by push_back() or push_front()
function, that function has no effects"

Then in page 480 section 23.2.1.3 - deque modifiers, item 2 says:
"Notes: if an exception is thrown other than by the copy constructor
or assignment operator of 'T', there are no effects."
(Here 'T' is the element type of the container).

My questions:
To me, there appears to be a difference between the statements in page
468 and page 480. However, since page 468 adds the words  "unless
otherwise specified (see 23.2.1.3 and 23.2.4.3) ...", we have to
consider only the item 2 in page 480. Am I correct ?
Yes.

Item 2 in page 480 section 23.2.1.3 - deque modifiers, says that "if
an exception is thrown other than by the copy constructor or
assignment operator of 'T', there are no effects." Given this, suppose
the copy ctor and assignment operator of 'T' do not throw any
exceptions, but due to non-availability of memory, suppose bad_alloc
exception is thrown by insert() or push_back() or push_front()
function. Then the original container on which these functions were
called, will be kept in tact(ie the container will not be modified at
all). Am I correct ?. Now suppose either the copy ctor or the
assignment operator of 'T' throws some exception. Then what will
happen to the container on which these functions were called ? Will it
be partially modified ?

For example, consider:
               container.insert(iter, first, last);
When this function is called, while inserting the elements in the the
range [first, last) into 'container', suppose an exception is thrown
by either the copy ctor or the assignment operator of the element
type. Then will the 'container' maintain its original state before the
insert() was called or will there be some extra elements added to the
'container' prior to the exception thrown ?

Kindly explain.

If a copy or assignment throws an exception, the container will be
left in a valid but unspecified state. A client who catches the
exception before the container is destructed may inspect the state of
the container, and may perform any operation on the container for
which the unspecified state meets all of the requirements for said
operation (e.g. container.size(), container.clear(), etc.).

-Howard
 
S

subramanian100in

* Howard Hinnant said:
If a copy or assignment throws an exception, the container will be
left in a valid but unspecified state. A client who catches the
exception before the container is destructed may inspect the state of
the container, and may perform any operation on the container for
which the unspecified state meets all of the requirements for said
operation (e.g. container.size(), container.clear(), etc.).

-Howard

Thanks for your reply. Kindly clarify the following portion in your
reply.

How do I inspect the state of the container(before it gets
destructed) ? Please give sample program.

What operation can be performed on the container, what are the
requirements of the unspecified state and how to check if it meets the
requirements ? Kindly explain with code sample.

Thanks
V.Subramanian
 
F

Francesco S. Carta

on said:
Thanks for your reply. Kindly clarify the following portion in your
reply.

How do I inspect the state of the container(before it gets
destructed) ? Please give sample program.

What operation can be performed on the container, what are the
requirements of the unspecified state and how to check if it meets the
requirements ? Kindly explain with code sample.


"Valid but unspecified" means that it is just valid as before, only that
there is no guarantee about the results of the operation that threw the
exception. The container could have been cleared, could have been left
completely unchanged, could have received only part of the data and so
forth.

Since it's a completely valid object, you can perform all the
(reasonable) operations as you would with any other object. Being able
to inspect it you can check whether any work has been done, and if so,
how much of it.

This is a silly example which very unlikely would return anything
different from zero:

#include <vector>
#include <exception>

int main() {
std::vector<int> v, w;
try {
w.push_back(42);
v = w;
} catch(std::exception) {
if(v.size()) {
// v.size() elements have been copied
// it is safe to access the elements:
return v[0];
}
if(w.size()) {
// w.size() elements have been inserted
// it is safe to access the elements:
return w[0];
}
}
return 0;
}
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top