473,399 Members | 4,177 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,399 software developers and data experts.

initialization in initializer list using copy constuctor?!?

Hi,

I wonder about the behaviour of como and icc on some very simple program. I
thought initializing members of classes, which are of class type, would be
'direct initialized' (as the standard says). But in the example below the
copy
constructor of Vec is executed when initializing the Ref object. Is this
standard conform? Doesn't it result in bad performance?

#include <iostream>

using namespace std;

class Vec
{
public:
Vec() { cout << "constructor" << endl;}
Vec(const Vec &v) { cout << "copy constructor" << endl; }
};

class Ref
{
public:
Ref() : vec_(Vec()) { }

private:
Vec vec_;
};

int
main()
{
Ref ref;

return 0;
}
regards,
alex
Jul 22 '05 #1
6 2961
On Fri, 09 Apr 2004 14:42:48 +0200, Alexander Stippler
<st**@mathematik.uni-ulm.de> wrote:
Hi,

I wonder about the behaviour of como and icc on some very simple program. I
thought initializing members of classes, which are of class type, would be
'direct initialized' (as the standard says). But in the example below the
copy
constructor of Vec is executed when initializing the Ref object. Is this
standard conform?
You're perhaps confusing "direct initialization" with "default
initialization". A line of code that invokes either of your two
constructors "directly" is "direct initialization. For example,

Vec v; // default initialization
Vec v(v2); // direct initialization
Vec v = v2; // not direct initialization, but still initialization
v = v2; // not initialization at all
Doesn't it result in bad performance? Not at all; you have to do what you have to do!

#include <iostream>

using namespace std;

class Vec
{
public:
Vec() { cout << "constructor" << endl;}
Vec(const Vec &v) { cout << "copy constructor" << endl; }
};

class Ref
{
public:
Ref() : vec_(Vec()) { }
If you had omitted the base initializer totally, or used it but omitted the
"Vec()" from inside the parens, it would have called the default
constructor.
-leor

private:
Vec vec_;
};

int
main()
{
Ref ref;

return 0;
}
regards,
alex


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
On Fri, 09 Apr 2004 14:42:48 +0200, Alexander Stippler
<st**@mathematik.uni-ulm.de> wrote:
Hi,

I wonder about the behaviour of como and icc on some very simple program. I
thought initializing members of classes, which are of class type, would be
'direct initialized' (as the standard says). But in the example below the
copy
constructor of Vec is executed when initializing the Ref object. Is this
standard conform?
You're perhaps confusing "direct initialization" with "default
initialization". A line of code that invokes either of your two
constructors "directly" is "direct initialization. For example,

Vec v; // default initialization
Vec v(v2); // direct initialization
Vec v = v2; // not direct initialization, but still initialization
v = v2; // not initialization at all
Doesn't it result in bad performance? Not at all; you have to do what you have to do!

#include <iostream>

using namespace std;

class Vec
{
public:
Vec() { cout << "constructor" << endl;}
Vec(const Vec &v) { cout << "copy constructor" << endl; }
};

class Ref
{
public:
Ref() : vec_(Vec()) { }
If you had omitted the base initializer totally, or used it but omitted the
"Vec()" from inside the parens, it would have called the default
constructor.
-leor

private:
Vec vec_;
};

int
main()
{
Ref ref;

return 0;
}
regards,
alex


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #3
>>class Ref
{
public:
Ref() : vec_(Vec()) { }


If you had omitted the base initializer totally, or used it but omitted
the "Vec()" from inside the parens, it would have called the default
constructor.
-leor


Consider Vec() replaced by e.g Vec(a,b,c). Why is it necessary to (1) create
Vec(a,b,c) and then (2) copy it into vec_ using the copy constructor?
Shouldn't it just directly initialize the vec_ object?
By the way, gcc does exactly this, como and icc not. This situation is IMO
comparable to copy constructors given a temporary class object, e.g.

Vec v(Vec());

would result in the direct initialization of v with the temporary Vec(), not
first create it and then copy it into v. Where is the difference?
Jul 22 '05 #4
>>class Ref
{
public:
Ref() : vec_(Vec()) { }


If you had omitted the base initializer totally, or used it but omitted
the "Vec()" from inside the parens, it would have called the default
constructor.
-leor


Consider Vec() replaced by e.g Vec(a,b,c). Why is it necessary to (1) create
Vec(a,b,c) and then (2) copy it into vec_ using the copy constructor?
Shouldn't it just directly initialize the vec_ object?
By the way, gcc does exactly this, como and icc not. This situation is IMO
comparable to copy constructors given a temporary class object, e.g.

Vec v(Vec());

would result in the direct initialization of v with the temporary Vec(), not
first create it and then copy it into v. Where is the difference?
Jul 22 '05 #5
On Fri, 09 Apr 2004 15:49:55 +0200, Alexander Stippler
<st**@mathematik.uni-ulm.de> wrote:
class Ref
{
public:
Ref() : vec_(Vec()) { }
If you had omitted the base initializer totally, or used it but omitted
the "Vec()" from inside the parens, it would have called the default
constructor.
-leor


First of all, sorry, I said "base initializer" when of course it is a
"member" initializer. No idea why my brain did that (not enough coffee yet,
I guess....)

Consider Vec() replaced by e.g Vec(a,b,c). Why is it necessary to (1) create
Vec(a,b,c) and then (2) copy it into vec_ using the copy constructor?
Okay, several issues here. First, are you aware that you do not need, in
/either/ case, to use the "temporary" syntax? IOW, you can either omit the
member initializer completely (which invokes vec_'s default constructor)
or, to pass the ctor args, use:
Ref() : vec_(a,b,c) {}

This results in the best performance imaginable. The fact that some
compilers take your version and optimize away the temporaries, but others
don't, is a QOI issue...and I wouldn't even say the ones that do not suffer
from lower QOI, but rather than their implementors chose to focus on other
features (perhaps "export" in the case of EDG, the authors of the Comeau
front-end) that were likely to make more of a difference to more folks.
Shouldn't it just directly initialize the vec_ object?
By the way, gcc does exactly this, como and icc not. This situation is IMO
comparable to copy constructors given a temporary class object, e.g.

Vec v(Vec());

would result in the direct initialization of v with the temporary Vec(), not
first create it and then copy it into v. Where is the difference?


QOI.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #6
On Fri, 09 Apr 2004 15:49:55 +0200, Alexander Stippler
<st**@mathematik.uni-ulm.de> wrote:
class Ref
{
public:
Ref() : vec_(Vec()) { }
If you had omitted the base initializer totally, or used it but omitted
the "Vec()" from inside the parens, it would have called the default
constructor.
-leor


First of all, sorry, I said "base initializer" when of course it is a
"member" initializer. No idea why my brain did that (not enough coffee yet,
I guess....)

Consider Vec() replaced by e.g Vec(a,b,c). Why is it necessary to (1) create
Vec(a,b,c) and then (2) copy it into vec_ using the copy constructor?
Okay, several issues here. First, are you aware that you do not need, in
/either/ case, to use the "temporary" syntax? IOW, you can either omit the
member initializer completely (which invokes vec_'s default constructor)
or, to pass the ctor args, use:
Ref() : vec_(a,b,c) {}

This results in the best performance imaginable. The fact that some
compilers take your version and optimize away the temporaries, but others
don't, is a QOI issue...and I wouldn't even say the ones that do not suffer
from lower QOI, but rather than their implementors chose to focus on other
features (perhaps "export" in the case of EDG, the authors of the Comeau
front-end) that were likely to make more of a difference to more folks.
Shouldn't it just directly initialize the vec_ object?
By the way, gcc does exactly this, como and icc not. This situation is IMO
comparable to copy constructors given a temporary class object, e.g.

Vec v(Vec());

would result in the direct initialization of v with the temporary Vec(), not
first create it and then copy it into v. Where is the difference?


QOI.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Michael McKnerney | last post by:
Hi, It seems I can influence how a base class is initialized beyond the 'normal' manner and I was wondering if someone can tell me why this. Here's my example. class A { public: A(int a=1,...
6
by: Alexander Stippler | last post by:
Hi, I wonder about the behaviour of como and icc on some very simple program. I thought initializing members of classes, which are of class type, would be 'direct initialized' (as the standard...
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
6
by: rona | last post by:
Hi everyone, Could someone tell us why the below code compiles and works? Is it legal in the constructor of class B? Cheers Ronny ---------------------------------
8
by: sarathy | last post by:
Hi, I read the following points in K&R "Section A8.7 Initialization". Seems like i have a problem with them. * All expressions in the initialization of constant object/array must be constant...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
2
by: dj3vande | last post by:
Is this code, as the complete contents of a translation unit, valid C90 that initializes the struct foo to contain copies of the values passed to bar()? -------- struct foo { int i; void *v;...
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?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
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.