From: Stephen Howe on
>I want to return by value and I am not worried about the performance
>of this. The opposition is worried about performance but has no
>figures or any other reason to back this up. It is just "more
>efficient". I have run into this attitude many many times. It is quite
>normal for people to argue for one construct over another in the name
>of performance without even knowing if perfornance is an issue in that
>area.

Why not have a named function approach?
Design the interface so that 2 choices are available, return by const reference
or value.
Within the class, the same internal method is called or return of internal data.
I only provide both if the return type is a heavyweight class, if it is
lightweight ora builtin type, just return by value is
provided.

You then leave it up to the programmer who uses your class to decide which
version to use.

I use the value version if the external assignee is long-lived or needs
independent existence from the class object.
I use the reference version if the external assignee is short-lived (e.g. I
might be writing the value to a log).

Stephen Howe

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Stephen Howe on
On Thu, 3 Dec 2009 02:36:09 CST, Andrew <marlow.andrew(a)googlemail.com> wrote:

>On 2 Dec, 22:00, Zachary Turner <divisorthe...(a)gmail.com> wrote:
>> On Dec 2, 7:06 am, Andrew <marlow.and...(a)googlemail.com> wrote:
>>
>> Maybe I'm overlooking something, but I see little reason to return by
>> value. What matters is whether you assign it to a const reference or
>> to a value.
>
>The view I am running into is that a function should return a const
>ref to a string so that it can be assigned to a const ref to a string.
>The argument is that doing this avoids string copying. I should have
>made that clearer.

Why cant you provide both and leave it up to the programmer who uses your class?

Stephen Howe

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Andrew on
On 3 Dec, 18:23, restor <akrze...(a)gmail.com> wrote:
> > Can anyone think of any other reasons to prefer returning a string by
> > value. I am encountering some opposition to this, mainly in the name
> > of performance. The performance has not been measured (of course) but
> > this is often the case with 'performance' arguments. Unfortunately,
> > "show me the figures" cuts no ice.
>
> Apart from performance (by-value can be faster as pointed out inhttp://cpp-next.com/archive/2009/08/want-speed-pass-by-value/) the
> simplest argument for by-value is elegance and clarity.
>
> string process( string name, string content );
>
> simply reads smoother than
>
> string const& process( string const& name, const string &
> content );

I think this thread is beginning to lose sight of my original
statement: it is the pros and cons of RETURNING a const ref to a
string. The discussion seems to be degenerating into the pros and cons
of passing objects (not just strings) by value instead of by const-
ref. That's NOT what I am talking about. Please, let's keep to the
discussion of the pros and cons of RETURNING const-ref to STRING
instead of returning string by value. Thanks.

In the signature for process, above, the two alternatives relevant to
this discussion are:

string process (const string& name, const string& content);

versus

const string& process (const string& name, const string& content);

-Andrew Marlow


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]