Re: Returning by value (here we go again!)

From: Howard Hinnant (hinnant_at_metrowerks.com)
Date: 10/01/04


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



Relevant Pages

  • Re: a method to make js have the ability to inherit
    ... but without the implied type-conversion of the string ... that uses the name of a specific constructor. ... programmer has no idea at all what types of object they are ... no reason for ever doing so. ...
    (comp.lang.javascript)
  • Re: Inherited Methods and such
    ... Now, what you want is to get in the constructor of some S derived from T, ... I have no problem with the semantics of object ... wrong to call it "dispatch on the operations of T." ... procedure Register; ...
    (comp.lang.ada)
  • Re: Newbie question: Writing your own class
    ... is made by a class's Create constructor method which allocates memory ... FDefinition: string; ... every other descendant of TObject ... Result:= FDefinition; ...
    (comp.lang.pascal.delphi.misc)
  • Re: About speed
    ... property Name: String read fName; ... constructor TProperty.Create; ... fValue: PropertyOfT_DataType; ... constructor Create(other: TPropertyOfT); overload; ...
    (borland.public.delphi.non-technical)
  • Re: a method to make js have the ability to inherit
    ... discriminating but without the implied type-conversion of the string ... makes no use of an object's - constructor - property anyway.) ... programs should serve some known purpose known to the programmer. ... var o = Father.prototype; ...
    (comp.lang.javascript)