473,889 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Mar 22 '07 #1
7 2504
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<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).

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<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
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
constructor s.
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
2696
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 possibility and because the languages do not specify the order of evaluation, doing so was an error. int B::f ( int i, int j = i + 1 ) { // j defaults to i + 1
21
4146
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
3975
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 parentheses. 1) "The order of evaluation of subexpressions is determined by the precedence and grouping of operators."
0
9961
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9803
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11187
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10783
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10439
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5825
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4248
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.