By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,180 Members | 1,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,180 IT Pros & Developers. It's quick & easy.

hmm.... two things to clarify...

P: n/a
Hi,

I have something that I'd like to clarify here.... there are two
issues. I have simple dummy class called base which (everything is
inlined just for readability) defines these methods...
friend base operator+ (const base& lhs, const base& rhs)
{
base tmp;
tmp.l = lhs.l + rhs.l;
cout << "friend base operator+ (const base& lhs, const base&
rhs)" << endl;
return tmp;
}

base& operator+(const base& rhs)
{
cout << " base& operator+(const base& rhs) " << endl;
this->l+=rhs.l;
return *this;
}
I have put cout statements in my default ctor and copy ctor to see
when they are called.

Now in my main() function, I have a routine thus;
base f(const base& b1, const base& b2)
{
cout << "base f(const base& b1, const base& b2)" << endl;
base tmp = b1 +b2;

return tmp;
};

My question is this. Within the function f above, I would expect to
see output indicating;

1) that the friend base operator+ (const base& lhs, const base& rhs)
function will be called first.

2) then the copy ctor will happen for base tmp = b1 +b2;

3) then a copy ctor will be called for the return tmp statement.

4) default dtor called for the temporary created upon f exit.
thats not what I'm seeing however, my gnu c++ compiler is showing;

1) default ctor called for base tmp = b1 +b2;
2) followed by friend base operator+ (const base& lhs, const base&
rhs) function
I don't see the ctor being called for the return value followed by the
dtor for the local tmp variable. Here's my thinking....

1) when doing declaration and assignment on the same line, the copy
ctor is called only if the rhs of "=" contains a single value. If
there's any operations to be done, then the default ctor is used and
then thats assigned into. i.e. base tmp = b1 +b2 doesn't call copy
ctor but base tmp = b2; would

2) The compiler is doing some funky optimisation wrt the copy of tmp
being made for the return value. This point is the one that concerns
me most. (yes, all my cout statements use endl at the end to flush the
buffer.

thanks a million

GrahamO
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
grahamo wrote:

thats not what I'm seeing however, my gnu c++ compiler is showing;

1) default ctor called for base tmp = b1 +b2;
2) followed by friend base operator+ (const base& lhs, const base&
rhs) function

I don't see the ctor being called for the return value followed by the
dtor for the local tmp variable. Here's my thinking....

1) when doing declaration and assignment on the same line, the copy
ctor is called only if the rhs of "=" contains a single value. If
there's any operations to be done, then the default ctor is used and
then thats assigned into. i.e. base tmp = b1 +b2 doesn't call copy
ctor but base tmp = b2; would

2) The compiler is doing some funky optimisation wrt the copy of tmp
being made for the return value. This point is the one that concerns
me most. (yes, all my cout statements use endl at the end to flush the
buffer.


2) is true.
The compiler is allowed to optimize away the copy ctor. It is explicitely
allowed to do so, even if the copy ctor contains side efefects, like
your output statements.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

P: n/a

"grahamo" <gr************@hotmail.com> wrote in message
news:79**************************@posting.google.c om...

</snip>
2) The compiler is doing some funky optimisation wrt the copy of tmp
being made for the return value. This point is the one that concerns
me most. (yes, all my cout statements use endl at the end to flush the
buffer.


Reference to Standard 12.8/15
"Whenever a temporary class object is copied using a copy constructor, and this
object and the copy have the same cv-unqualified type, an implementation is
permitted to treat the original and the copy as two different ways of referring
to the same object and not perform a copy at all, even if the class copy
constructor or destructor have side effects.For a function with a class return
type, if the expression in the return statement is the name of a local object,
and the cv-unqualified type of the local object is the same as the function
return type, an implementation is permitted to omit creating the temporary
object to hold the function return value, even if the class copy constructor or
destructor has side effects. In these cases, the object is destroyed at the
later of times when the original and the copy would have been destroyed without
the optimization"

-Sharad
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.