Boost logo

Boost :

From: Samuel Krempp (krempp_at_[hidden])
Date: 2004-05-23 16:23:01


On Sun, 2004-05-23 at 19:03, Gennadiy Rozental wrote:
> > About the code :
> > I'm wondering, why have the str() function return a const-reference to
> > string rather than a value, like stringstream does ?
>
> Is there a reason to return it by value?

I think both possibilities don't make any difference in performance (as
long as the return-by-value is optimized and does not lead to an extra
string copy). And the semantics difference is not really substential.

Personnally the only concrete reason to return by value I could think of
is it's simpler with value : the stream would not need to store m_str
(which is redundant with the buffer inside the stringstream /
strstream).

I don't know stringstream's designers motivation, but they faced the
same choice. That's why I thought you found reasons against
stringstream's by-value str() return.
If there isn't any, I'd go for the by-value str(), to keep the user
comfortable with any habit she might have. (like, directly modifying the
result of the str(). probably not too common, but who knows..)
In fact, it seems that if the user wants to modify the string obtained
from str(), the return-by-value design is the best (in case of RVO),
whereas the design with the m_str stored string implies an unnecessary
copy.

-- 
Samuel

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk