Boost logo

Boost :

Subject: Re: [boost] [algorithm] to_lower_copy / to_upper_copy with no output param assumes sequence to be same as input
From: Sebastian Karlsson (sairony_at_[hidden])
Date: 2011-06-23 07:26:21


2011/6/23 Olaf van der Spek <ml_at_[hidden]>:
> On Wed, Jun 22, 2011 at 3:05 PM, Sebastian Karlsson <sairony_at_[hidden]> wrote:
>> It would seem: std::string foo = boost::algorithm::to_lower_copy<
>> std::string >( "bar" ); isn't valid. Instead one has to do for
>> example: std::string foo = boost::algorithm::to_lower_copy(
>> std::string( "bar" ) ), which while perhaps just as terse and natural
>> creates an extra unneeded temporary. I propose the following addition,
>> as well as one for to_upper_copy:
>>
>>                template<typename OutputSequenceT, typename RangeT>
>>                inline OutputSequenceT
>>                        to_lower_copy(
>>                        const RangeT& Input,
>>                        const std::locale& Loc=std::locale())
>>                {
>>                        OutputSequenceT Output( ::boost::begin( Input ), ::boost::end( Input ) );
>
> Doesn't this also do an unnecessary copy?
>
> Olaf

It does, but it uses one less which as stated is a prime candidate for
NRVO. The current approach needs one temporary for the return value as
well as a temporary for argument conversion, which is basically only
there to inform about the return type.

Kind regards,
Sebastian Karlsson


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