order of evaluation of arguments to constructors

P

Peter

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)
{
}
};
 
J

James Curran

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).
 
P

Peter

James said:
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
 
I

Ian Collins

Peter said:
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!
 
V

Victor Bazarov

Peter said:
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
 
M

Mark P

Peter said:
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.
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top