Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > order of evaluation of arguments to constructors

Reply
Thread Tools

order of evaluation of arguments to constructors

 
 
Peter
Guest
Posts: n/a
 
      03-22-2007
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)
{
}
};

 
Reply With Quote
 
 
 
 
James Curran
Guest
Posts: n/a
 
      03-22-2007
On Mar 22, 4:48 pm, "Peter" <(E-Mail Removed)> 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).

 
Reply With Quote
 
 
 
 
Peter
Guest
Posts: n/a
 
      03-22-2007

James Curran wrote:
> On Mar 22, 4:48 pm, "Peter" <(E-Mail Removed)> 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

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-22-2007
Peter wrote:
> James Curran wrote:
>
>>On Mar 22, 4:48 pm, "Peter" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      03-22-2007

Ian Collins wrote:
> Peter wrote:
> > James Curran wrote:
> >
> >>On Mar 22, 4:48 pm, "Peter" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      03-22-2007

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.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-22-2007
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


 
Reply With Quote
 
Mark P
Guest
Posts: n/a
 
      03-22-2007
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Order of evaluation of arguments in a subroutine Krishna Chaitanya Perl Misc 1 12-09-2010 07:49 PM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM
Order of evaluation of function arguments dragoncoder C Programming 21 12-23-2005 07:08 PM
Constructors that call other Constructors Dave Rudolf C++ 12 02-06-2004 03:26 PM
order of evaluation for method arguments Michael Garriss Ruby 6 09-13-2003 01:20 PM



Advertisments