order of evaluation of arguments to constructors

Discussion in 'C++' started by Peter, Mar 22, 2007.

  1. Peter

    Peter Guest

    I know the order of construction of member and base class objects.
    My question is the following:
    Is the order of evaluation of argument lists for these constructors
    also defined?
    E.g. can I assume that the following code is exceptions safe?
    Assuming that the constructor of A, B or C may throw?
    Can I assume that B is created after the constructor of m_sA has been
    called?


    struct D
    { std::auto_ptr<A> m_sA;
    std::auto_ptr<B> m_sB;
    std::auto_ptr<C> m_sC;
    D(void)
    :m_sA(new A),
    m_sB(new B),
    m_sC(new C)
    {
    }
    };
     
    Peter, Mar 22, 2007
    #1
    1. Advertising

  2. Peter

    James Curran Guest

    On Mar 22, 4:48 pm, "Peter" <> wrote:
    > Is the order of evaluation of argument lists for these constructors
    > also defined?
    > struct D
    > { std::auto_ptr<A> m_sA;
    > std::auto_ptr<B> m_sB;
    > std::auto_ptr<C> m_sC;
    > D(void)
    > :m_sA(new A),
    > m_sB(new B),
    > m_sC(new C)
    > {
    > }
    > };


    Data member are constructed in the order that are listed in the
    class defination (not necessarily the order in the constructor).
    Hence in your example, regardless of how the constructor is written,
    m_sA will always be constructed first, then m_sB, and finally m_sC.
    (Similarly, regardless of how the object is constructed, m_sC will
    always be destructed first, then m_sB and them m_sA).
     
    James Curran, Mar 22, 2007
    #2
    1. Advertising

  3. Peter

    Peter Guest

    James Curran wrote:
    > On Mar 22, 4:48 pm, "Peter" <> wrote:
    > Data member are constructed in the order that are listed in the
    > class defination (not necessarily the order in the constructor).
    > Hence in your example, regardless of how the constructor is written,
    > m_sA will always be constructed first, then m_sB, and finally m_sC.
    > (Similarly, regardless of how the object is constructed, m_sC will
    > always be destructed first, then m_sB and them m_sA).



    I said I know the order of construction.
    My question was regarding the order of the argument list of the
    constructors.
    Visual C++ and gnu-c++ both execute the code like that:

    A
    auto_ptr
    B
    auto_ptr
    C
    auto_ptr
    D

    Can I assume that this is always like that?
    Or could it be that some compiler does

    A
    B
    C
    auto_ptr
    auto_ptr
    auto_ptr
    D
     
    Peter, Mar 22, 2007
    #3
  4. Peter

    Ian Collins Guest

    Peter wrote:
    > James Curran wrote:
    >
    >>On Mar 22, 4:48 pm, "Peter" <> wrote:
    >> Data member are constructed in the order that are listed in the
    >>class defination (not necessarily the order in the constructor).
    >>Hence in your example, regardless of how the constructor is written,
    >>m_sA will always be constructed first, then m_sB, and finally m_sC.
    >>(Similarly, regardless of how the object is constructed, m_sC will
    >>always be destructed first, then m_sB and them m_sA).

    >
    > I said I know the order of construction.
    > My question was regarding the order of the argument list of the
    > constructors.


    Which is exactly what James answered!

    --
    Ian Collins.
     
    Ian Collins, Mar 22, 2007
    #4
  5. Peter

    Peter Guest

    Ian Collins wrote:
    > Peter wrote:
    > > James Curran wrote:
    > >
    > >>On Mar 22, 4:48 pm, "Peter" <> wrote:
    > >> Data member are constructed in the order that are listed in the
    > >>class defination (not necessarily the order in the constructor).
    > >>Hence in your example, regardless of how the constructor is written,
    > >>m_sA will always be constructed first, then m_sB, and finally m_sC.
    > >>(Similarly, regardless of how the object is constructed, m_sC will
    > >>always be destructed first, then m_sB and them m_sA).

    > >
    > > I said I know the order of construction.
    > > My question was regarding the order of the argument list of the
    > > constructors.

    >
    > Which is exactly what James answered!



    nope -- his answer would apply to both construction flows I gave as an
    example.
     
    Peter, Mar 22, 2007
    #5
  6. Peter

    Peter Guest

    Ian Collins wrote:
    > >> Data member are constructed in the order that are listed in the
    > >>class defination (not necessarily the order in the constructor).
    > >>Hence in your example, regardless of how the constructor is written,
    > >>m_sA will always be constructed first, then m_sB, and finally m_sC.
    > >>(Similarly, regardless of how the object is constructed, m_sC will
    > >>always be destructed first, then m_sB and them m_sA).

    > >
    > > I said I know the order of construction.
    > > My question was regarding the order of the argument list of the
    > > constructors.

    >
    > Which is exactly what James answered!



    I meant to say:
    His answer would not distinguish between the two different
    construction flows I offered.
     
    Peter, Mar 22, 2007
    #6
  7. Peter wrote:
    > I know the order of construction of member and base class objects.
    > My question is the following:
    > Is the order of evaluation of argument lists for these constructors
    > also defined?
    > E.g. can I assume that the following code is exceptions safe?
    > Assuming that the constructor of A, B or C may throw?
    > Can I assume that B is created after the constructor of m_sA has been
    > called?


    What do you think the difference is between "order of construction
    of member objects" and "evaluation of argument lists"? Does the comma
    between initialisers in the list look like a comma between function
    arguments (order of evaluation of which is unspecified)? If so, why
    doesn't it look like the comma between objects in a declaration
    statement:

    int *sA(new A), *sB(new B), *sC(new C);

    ? OK, I'll stop asking and just say it: the comma between the member
    initialisers in the constructor initialiser list is the same as the
    one between objects in a declaration statement -- and has the same
    trait -- there is a sequence point at each comma.

    > struct D
    > { std::auto_ptr<A> m_sA;
    > std::auto_ptr<B> m_sB;
    > std::auto_ptr<C> m_sC;
    > D(void)
    > :m_sA(new A),
    > m_sB(new B),
    > m_sC(new C)
    > {
    > }
    > };


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 22, 2007
    #7
  8. Peter

    Mark P Guest

    Peter wrote:
    > Ian Collins wrote:
    >>>> Data member are constructed in the order that are listed in the
    >>>> class defination (not necessarily the order in the constructor).
    >>>> Hence in your example, regardless of how the constructor is written,
    >>>> m_sA will always be constructed first, then m_sB, and finally m_sC.
    >>>> (Similarly, regardless of how the object is constructed, m_sC will
    >>>> always be destructed first, then m_sB and them m_sA).
    >>> I said I know the order of construction.
    >>> My question was regarding the order of the argument list of the
    >>> constructors.

    >> Which is exactly what James answered!

    >
    >
    > I meant to say:
    > His answer would not distinguish between the two different
    > construction flows I offered.
    >


    Victor already answered your question but I'll add the relevant passage
    from the Standard:

    12.6.2.3:

    There is a sequence point (1.9) after the initialization of each base
    and member. The expression-list of a mem-initializer is evaluated as
    part of the initialization of the corresponding base or member.
     
    Mark P, Mar 22, 2007
    #8
    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. Ilias Lazaridis
    Replies:
    2
    Views:
    401
    Ilias Lazaridis
    Apr 24, 2005
  2. dragoncoder

    Order of evaluation of function arguments

    dragoncoder, Dec 21, 2005, in forum: C Programming
    Replies:
    21
    Views:
    868
    Keith Thompson
    Dec 23, 2005
  3. Michael Garriss

    order of evaluation for method arguments

    Michael Garriss, Sep 13, 2003, in forum: Ruby
    Replies:
    6
    Views:
    143
  4. Ilias Lazaridis
    Replies:
    74
    Views:
    788
    Ilias Lazaridis
    Apr 4, 2005
  5. Krishna Chaitanya

    Order of evaluation of arguments in a subroutine

    Krishna Chaitanya, Dec 9, 2010, in forum: Perl Misc
    Replies:
    1
    Views:
    84
    Keith Thompson
    Dec 9, 2010
Loading...

Share This Page