Re: Returning by value (here we go again!)
From: Howard Hinnant (hinnant_at_metrowerks.com)
Date: 10/01/04
- Next message: Capstar: "Re: boost::checked delete"
- Previous message: dingounan_at_163.com: "how to use boost.thread"
- In reply to: JKop: "Returning by value (here we go again!)"
- Next in thread: Chris Theis: "Re: Returning by value (here we go again!)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 01 Oct 2004 13:39:50 GMT
In article <Ara7d.32561$Z14.11712@news.indigo.ie>,
JKop <NULL@NULL.NULL> wrote:
> First of all, I think it would be handy if we had a new type of "return"
> statement in C++, one which makes it so that the actual object is returned
> (no copy made) and as such the copy constructor wouldn't need to be
> public... allowing the likes of the following:
>
> std::ostream Blah()
> {
> std::ostream poo;
>
> return_actual_object poo;
> }
I agree, at least partly. This can be achieved with move semantics:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1690.html
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm
which unfortunately is not part of C++ today. If you would like to see
move semantics become part of C++, make sure your national
representative on the C++ committee knows that.
Movable, but non-copyable types with non-cv qualified auto storage
should be returnable by value from functions. For example see:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm#Moving%
20from%20local%20values
std::streams could be made movable, and remain non-copyable.
> Anyway...
>
> To achieve this, at the moment I'm using auto_ptr, as follows:
>
> auto_ptr<AnyClass> Blah()
> {
> auto_ptr<AnyClass> poo( new AnyClass );
>
> return poo;
> }
>
> int main()
> {
> auto_ptr<AnyClass> const &milk = Blah();
>
> //now work with it as I please!
> //and if I want reference syntax:
>
> AnyClass const& cheese = *milk;
>
> cheese.MemberFunction();
> }
<nod> Today's workarounds are ugly.
> AAnnyywwaayy... looking through a C++ reference today I was sort of shocked
> to see:
>
> basic_string::substr
> basic_string substr(size_type off = 0,
> size_type count = npos) const;
> The member function returns an object whose controlled sequence is a copy of
> up to count elements of the controlled sequence beginning at position off.
>
> It returns by value! AAAAAAaaahhhhhhhh! throw Inefficiency(); !
A good compiler can optimize away this copy if the client is using
substr to construct another string. But if it is using substr to assign
into an existing string a temporary is still created.
It would be a lot more palatable with move semantics. The temporary
could still be created, but then it would be moved from, instead of
copied from.
-Howard
- Next message: Capstar: "Re: boost::checked delete"
- Previous message: dingounan_at_163.com: "how to use boost.thread"
- In reply to: JKop: "Returning by value (here we go again!)"
- Next in thread: Chris Theis: "Re: Returning by value (here we go again!)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|