Discussion:
returning something from the stack in a function
(too old to reply)
acchow
2011-06-30 00:57:56 UTC
Permalink
In the class notes, (Page 126 of the Corrected Course Notes), the
implementation of the Complex class has an operator+ overload
implemented. But it looks like this:

return Complex( a.impl.re + b.impl.re, a.impl.im + b.impl.im );

It is returning something that is transient? How is this working?
acchow
2011-06-30 01:03:41 UTC
Permalink
Hmm...it seems the implementation of the Complex constructor creates an
impl on the heap...

So why doesn't the operator+ return a pointer?
Post by acchow
In the class notes, (Page 126 of the Corrected Course Notes), the
implementation of the Complex class has an operator+ overload
return Complex( a.impl.re + b.impl.re, a.impl.im + b.impl.im );
It is returning something that is transient? How is this working?
Jeremy Roman
2011-06-30 02:35:40 UTC
Permalink
Post by acchow
Hmm...it seems the implementation of the Complex constructor creates an
impl on the heap...
So why doesn't the operator+ return a pointer?
Post by acchow
In the class notes, (Page 126 of the Corrected Course Notes), the
implementation of the Complex class has an operator+ overload
return Complex( a.impl.re + b.impl.re, a.impl.im + b.impl.im );
It is returning something that is transient? How is this working?
It returns the object itself (again, not a pointer), just as you might
return a struct or even a primitive type. If it's clearer, the line you
pasted is equivalent to:

Complex c(a.impl.re + b.impl.re, a.impl.im + b.impl.im);
return c;

The location is impl is not relevant, as it is expected that an instance
of the Complex class knows how to allocate it if needed when it is
constructed.

Does that make sense?
--
Jeremy Roman
Student, Computer Science
University of Waterloo
acchow
2011-06-30 04:44:43 UTC
Permalink
Post by Jeremy Roman
Post by acchow
Hmm...it seems the implementation of the Complex constructor creates an
impl on the heap...
So why doesn't the operator+ return a pointer?
Post by acchow
In the class notes, (Page 126 of the Corrected Course Notes), the
implementation of the Complex class has an operator+ overload
return Complex( a.impl.re + b.impl.re, a.impl.im + b.impl.im );
It is returning something that is transient? How is this working?
It returns the object itself (again, not a pointer), just as you might
return a struct or even a primitive type. If it's clearer, the line you
Complex c(a.impl.re + b.impl.re, a.impl.im + b.impl.im);
return c;
The location is impl is not relevant, as it is expected that an instance
of the Complex class knows how to allocate it if needed when it is
constructed.
Does that make sense?
I see...

So, at return, is the copy constructor called? Because it looks like
when you instantiate c, the instance exists in the stack of the
function. How can the caller still use it?
acchow
2011-06-30 04:57:18 UTC
Permalink
Post by acchow
Post by Jeremy Roman
Post by acchow
Hmm...it seems the implementation of the Complex constructor creates an
impl on the heap...
So why doesn't the operator+ return a pointer?
Post by acchow
In the class notes, (Page 126 of the Corrected Course Notes), the
implementation of the Complex class has an operator+ overload
return Complex( a.impl.re + b.impl.re, a.impl.im + b.impl.im );
It is returning something that is transient? How is this working?
It returns the object itself (again, not a pointer), just as you might
return a struct or even a primitive type. If it's clearer, the line you
Complex c(a.impl.re + b.impl.re, a.impl.im + b.impl.im);
return c;
The location is impl is not relevant, as it is expected that an instance
of the Complex class knows how to allocate it if needed when it is
constructed.
Does that make sense?
I see...
So, at return, is the copy constructor called? Because it looks like
when you instantiate c, the instance exists in the stack of the
function. How can the caller still use it?
Oh found an answer:

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.9

"Does return-by-value mean extra copies and extra overhead?

Not necessarily.

All(?) commercial-grade compilers optimize away the extra copy"

And it has examples in there
acchow
2011-06-30 07:09:54 UTC
Permalink
Post by acchow
Post by acchow
Post by Jeremy Roman
Post by acchow
Hmm...it seems the implementation of the Complex constructor creates an
impl on the heap...
So why doesn't the operator+ return a pointer?
Post by acchow
In the class notes, (Page 126 of the Corrected Course Notes), the
implementation of the Complex class has an operator+ overload
return Complex( a.impl.re + b.impl.re, a.impl.im + b.impl.im );
It is returning something that is transient? How is this working?
It returns the object itself (again, not a pointer), just as you might
return a struct or even a primitive type. If it's clearer, the line you
Complex c(a.impl.re + b.impl.re, a.impl.im + b.impl.im);
return c;
The location is impl is not relevant, as it is expected that an instance
of the Complex class knows how to allocate it if needed when it is
constructed.
Does that make sense?
I see...
So, at return, is the copy constructor called? Because it looks like
when you instantiate c, the instance exists in the stack of the
function. How can the caller still use it?
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.9
"Does return-by-value mean extra copies and extra overhead?
Not necessarily.
All(?) commercial-grade compilers optimize away the extra copy"
And it has examples in there
Seems this is a widely followed design pattern, as Bjarne Stroustrup
himself suggests to "do as the ints do" (i.e. make your class behave
like a primitive type). Of particular relevance, make use of automatic
objects as much as possible.
Jeremy Roman
2011-06-30 11:14:10 UTC
Permalink
Post by acchow
Post by acchow
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.9
"Does return-by-value mean extra copies and extra overhead?
Not necessarily.
All(?) commercial-grade compilers optimize away the extra copy"
And it has examples in there
Seems this is a widely followed design pattern, as Bjarne Stroustrup
himself suggests to "do as the ints do" (i.e. make your class behave
like a primitive type). Of particular relevance, make use of automatic
objects as much as possible.
There are advantages and disadvantages to imitating primitive types. One
advantage is that you can end up with very concise code; one
disadvantage is that it may be difficult to figure out what a piece of
code does or what its time complexity is due to the level of indirection
between the code calling it and the code it compiles down to. It's a
matter of style.

If you want to see what happens (which constructor is called, etc.), you
can actually try writing a sample of code that does the thing that you
are thinking about, and putting logging statements in the functions of
interest.
--
Jeremy Roman
Student, Computer Science
University of Waterloo
Loading...