473,408 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

order of evaluation of arguments to constructors

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
7 2474
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

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
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

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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following...
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
54
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.