exceptions and container modifiers

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Jul 23, 2010.

  1. , India

    , India Guest

    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
    , India, Jul 23, 2010
    #1
    1. Advertising

  2. On Jul 23, 3:40 am, ", India"
    <> wrote:
    > 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
    Howard Hinnant, Jul 23, 2010
    #2
    1. Advertising

  3. , India

    , India Guest

    * Howard Hinnant <> wrote:

    > 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
    , India, Jul 24, 2010
    #3
  4. , India <>, on
    23/07/2010 18:51:50, wrote:

    > * Howard Hinnant<> wrote:
    >
    >> 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.



    "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;
    }

    --
    FSC - http://userscripts.org/scripts/show/59948
    http://fscode.altervista.org - http://sardinias.com
    Francesco S. Carta, Jul 24, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Razvan
    Replies:
    11
    Views:
    895
    Andrew Thompson
    Jul 17, 2004
  2. Indudhar
    Replies:
    4
    Views:
    364
    Patrick May
    Oct 21, 2005
  3. enums and modifiers

    , Nov 14, 2007, in forum: Java
    Replies:
    5
    Views:
    371
    Roedy Green
    Nov 15, 2007
  4. , India
    Replies:
    8
    Views:
    292
    Bo Persson
    Jul 27, 2010
  5. Miquel

    Class & modifiers modifiers

    Miquel, Jan 25, 2007, in forum: Ruby
    Replies:
    4
    Views:
    160
    Miquel
    Jan 26, 2007
Loading...

Share This Page