By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

order of evaluation of arguments to constructors

P: n/a
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<Am_sA;
std::auto_ptr<Bm_sB;
std::auto_ptr<Cm_sC;
D(void)
:m_sA(new A),
m_sB(new B),
m_sC(new C)
{
}
};

Mar 22 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Mar 22, 4:48 pm, "Peter" <pet...@xpedion.comwrote:
Is the order of evaluation of argument lists for these constructors
also defined?
struct D
{ std::auto_ptr<Am_sA;
std::auto_ptr<Bm_sB;
std::auto_ptr<Cm_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).

Mar 22 '07 #2

P: n/a

James Curran wrote:
On Mar 22, 4:48 pm, "Peter" <pet...@xpedion.comwrote:
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

Mar 22 '07 #3

P: n/a
Peter wrote:
James Curran wrote:
>>On Mar 22, 4:48 pm, "Peter" <pet...@xpedion.comwrote:
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.
Mar 22 '07 #4

P: n/a

Ian Collins wrote:
Peter wrote:
James Curran wrote:
>On Mar 22, 4:48 pm, "Peter" <pet...@xpedion.comwrote:
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.

Mar 22 '07 #5

P: n/a

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.

Mar 22 '07 #6

P: n/a
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<Am_sA;
std::auto_ptr<Bm_sB;
std::auto_ptr<Cm_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
Mar 22 '07 #7

P: n/a
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.
Mar 22 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.