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

NRV question

P: n/a
A sample in <inside c++ object model>:

X bar()
{
X xx;
//...
return xx;
}

void bar(X &__result)
{
__result.X::X();
//...
return;
}

Why X::X() is called inside bar? I think it should be called before bar, for
better semantic.To always reset a input parameter (by calling its
constructor here) is senseless.

Is the latter better than the former anyway?
X x0=bar();
1. X::X() is called inside bar
X x0;
bar(x0);
2. outside
X x0;
x0.X::X();
bar(x0);
Sep 16 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
jronald wrote:
>
Why X::X() is called inside bar? I think it should be called before bar, for
better semantic.To always reset a input parameter (by calling its
constructor here) is senseless.
The compiler doesn't know at the point where it calls bar() how bar is
going to create the returned object. Suppose it does this:

X bar()
{
X xx(3);
return xx;
}

Now default constructing the return object at the point of the call is
simply wrong.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Sep 16 '06 #2

P: n/a
I was a little confused just now. You are right.
It's the function generated the object.
The complier treats "X x0;" a memory allocation only, which is different
from C++ semantic. In C++ semantic, "X x0" imply a call to the default
constructor.

"Pete Becker" <pe********@acm.org>
??????:wY******************************@giganews.c om...
jronald wrote:
>>
Why X::X() is called inside bar? I think it should be called before bar,
for better semantic.To always reset a input parameter (by calling its
constructor here) is senseless.

The compiler doesn't know at the point where it calls bar() how bar is
going to create the returned object. Suppose it does this:

X bar()
{
X xx(3);
return xx;
}

Now default constructing the return object at the point of the call is
simply wrong.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
For more information about this book, see www.petebecker.com/tr1book.

Sep 16 '06 #3

P: n/a
jronald wrote:
It's the function generated the object.
The complier treats "X x0;" a memory allocation only, which is different
from C++ semantic. In C++ semantic, "X x0" imply a call to the default
constructor.
Right. You have to be extra careful in discussions about what's going on
behind the scenes, because the compiler can do things that are different
from what the code suggests, provided the behavior of the program is the
same.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Sep 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.