class C
{
public:
C() { cout << "ctor" << endl; }
C(const C & rhs) { cout << "copy ctor" << endl; }
~C() { cout << "dtor" << endl; }
};
C c0; // A
C c1 = C(); // B
I could imagine, that line B causes the creation of a temporary object which
is used to intantiate c1 by means of the copy ctor. But it's not the case,
lines A and B cause the same (tested on several compilers). Is this
required? Or is the compiler free which way to do that? Is it optimization?
Thanks!
Robert Sturzenegger 6 1619
"Robert Sturzenegger" <ro*****************@abc.ch> wrote in message
news:3f******@news.swissonline.ch... class C { public: C() { cout << "ctor" << endl; } C(const C & rhs) { cout << "copy ctor" << endl; } ~C() { cout << "dtor" << endl; } };
C c0; // A C c1 = C(); // B
I could imagine, that line B causes the creation of a temporary object
which is used to intantiate c1 by means of the copy ctor. But it's not the case, lines A and B cause the same (tested on several compilers). Is this required? Or is the compiler free which way to do that? Is it
optimization? Thanks! Robert Sturzenegger
When you write A a = x; the effect is the same as A a(x); provided x is an
expression of the same type as A. Otherwise it is a construction of a
temporary object. This is a requirement, not an optional optimization.
john
"John Harrison" <jo*************@hotmail.com> wrote in message
news:bg************@ID-196037.news.uni-berlin.de... "Robert Sturzenegger" <ro*****************@abc.ch> wrote in message news:3f******@news.swissonline.ch... class C { public: C() { cout << "ctor" << endl; } C(const C & rhs) { cout << "copy ctor" << endl; } ~C() { cout << "dtor" << endl; } };
C c0; // A C c1 = C(); // B
I could imagine, that line B causes the creation of a temporary object which is used to intantiate c1 by means of the copy ctor. But it's not the
case, lines A and B cause the same (tested on several compilers). Is this required? Or is the compiler free which way to do that? Is it optimization? Thanks! Robert Sturzenegger
When you write A a = x; the effect is the same as A a(x); provided x is an expression of the same type as A. Otherwise it is a construction of a temporary object. This is a requirement, not an optional optimization.
john
What I said is true, but I'm not sure it answers your questions, as it only
means
C c1 = C();
is equivalent to
C c1(C());
Not sure what happens from that point on, sorry.
john
"Robert Sturzenegger" <ro*****************@abc.ch> wrote in message
news:3f******@news.swissonline.ch... class C { public: C() { cout << "ctor" << endl; } C(const C & rhs) { cout << "copy ctor" << endl; } ~C() { cout << "dtor" << endl; } };
C c0; // A C c1 = C(); // B
I could imagine, that line B causes the creation of a temporary object
which is used to intantiate c1 by means of the copy ctor. But it's not the case, lines A and B cause the same (tested on several compilers). Is this required? Or is the compiler free which way to do that? Is it
optimization?
Seems like compiler optimisation to me.
IMO, C c1(C()) may bypass the copy constructor and directly make the
temporary object c1.
--
With best wishes,
J.Schafer
"John Harrison" <jo*************@hotmail.com> wrote in message news:<bg************@ID-196037.news.uni-berlin.de>... "John Harrison" <jo*************@hotmail.com> wrote in message news:bg************@ID-196037.news.uni-berlin.de... "Robert Sturzenegger" <ro*****************@abc.ch> wrote in message news:3f******@news.swissonline.ch... class C { public: C() { cout << "ctor" << endl; } C(const C & rhs) { cout << "copy ctor" << endl; } ~C() { cout << "dtor" << endl; } };
C c0; // A C c1 = C(); // B
I could imagine, that line B causes the creation of a temporary object which is used to intantiate c1 by means of the copy ctor. But it's not the case, lines A and B cause the same (tested on several compilers). Is this required? Or is the compiler free which way to do that? Is it optimization? Thanks! Robert Sturzenegger
When you write A a = x; the effect is the same as A a(x); provided x is an expression of the same type as A. Otherwise it is a construction of a temporary object. This is a requirement, not an optional optimization.
john
What I said is true, but I'm not sure it answers your questions, as it only means
C c1 = C();
is equivalent to
C c1(C());
Not sure what happens from that point on, sorry.
john
I believe the compiler is allowed to optimise away the construction of
the temporary C and default construct c1 directly.
GJD
Robert Sturzenegger wrote: class C { public: C() { cout << "ctor" << endl; } C(const C & rhs) { cout << "copy ctor" << endl; } ~C() { cout << "dtor" << endl; } };
C c0; // A C c1 = C(); // B
I could imagine, that line B causes the creation of a temporary object which is used to intantiate c1 by means of the copy ctor. But it's not the case, lines A and B cause the same (tested on several compilers). Is this required? Or is the compiler free which way to do that? Is it optimization?
Your imagination is right: the compiler has to do this and it also has to check,
if this path is going to work (by eg. checking the accessability of the copy constructor).
But: The compiler is also allowed to optimize the call of the copy constructor away.
This is explicitely allowed by the C++ standard, even in the case that the copy
constructor has side effects as in your case.
--
Karl Heinz Buchegger kb******@gascad.at
Thank you for your responses!
Robert This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Tony Johansson |
last post by:
Hello Experts!
I have the following Array template class see below.
I execute these three statements
statement 1: Array<int> x(5);
statement 2: cin >>x;
statement 3: Array<int>::element_type ...
|
by: mlimber |
last post by:
This is a repost (with slight modifications) from
comp.lang.c++.moderated in an effort to get some response.
I am using Loki's Factory as presented in _Modern C++ Design_ for
message passing in...
|
by: Rangi Keen |
last post by:
I am instantiating an XmlSerializer using the XmlSerializer(Type)
constructor. This works most of the time, but sometimes I get a timeout
during the process. I'm using the same type in all cases...
|
by: cody |
last post by:
would it be possible to allow an extended syntax for Object instantiation so
that instead of
Hashtable<string,int> table = new Hashtable<string,int>(10, 10f);
I can write:
...
|
by: Anders Borum |
last post by:
Hello!
When declaring variables without immediately assigning a value, the CLR
(please correct here) assigns a default value according to the (reference)
type of the variable (e.g. string =...
|
by: Agoston Bejo |
last post by:
Hi! This is the smallest example I was able to create to demonstratet the
problem. It seems that a certain kind of object instantiation gets parsed as
a function definition (or whatever) by the...
|
by: awood45 |
last post by:
I ran into a bizarre crashing of one of my programs earlier today, and
noticed my IE 6 was messed up (maybe this is important, maybe it isn't,
but it coincided with this not working). After fixing...
|
by: peifeng_w |
last post by:
Hi, try the following code with flag=0/1/2.
#include<iostream>
using namespace std;
#define flag 2//option:0,1,2
class C
{
|
by: WittyGuy |
last post by:
Hi all,
All I want to achieve is restricting the object instantiation in stack
and allowing the application to instantiate the object only in heap
using new operator. How to achieve this?
I...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
| |