473,785 Members | 2,910 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 << "constructo r" << 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 3017
On Fri, 09 Apr 2004 14:42:48 +0200, Alexander Stippler
<st**@mathemati k.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 << "constructo r" << 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**@mathemati k.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 << "constructo r" << 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**@mathemati k.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**@mathemati k.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
9464
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, int b=1, int c=1) : _a(a), _b(b), _c(c) {}
6
371
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 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>
6
13386
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
2494
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
399
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 expression. * Expressions in the initializer for an auto or register object or array must likewise be constant expressions if the initializer is a brace enclosed list.
1
3537
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 following (with incomplete succes): I want in a given namespace Parameters a list of "initializers" (which are objects derived from a simple interface that can be implemented anywhere, and are used to define which parameters the program will take at...
23
3663
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 the built-in types, the value is usually garbage. Is this right? However, I'm a bit confused about value-initialization, when does it happen, and what kind of values are assigned to objects?
4
3714
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 value-initialization. I think default-init calls default constructor for class objects and sets garbage values to PODs. Value-init also calls default constructor for class objects and sets 0s to POD types. This is what I've learned from the books (especially...
2
3895
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
9645
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
10327
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
10151
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
9950
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
8973
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...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
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
4053
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
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.