Boost logo

Boost :

Subject: Re: [boost] Move semantics for std::string?
From: Jeffrey Lee Hellrung, Jr. (jeffrey.hellrung_at_[hidden])
Date: 2012-02-16 18:30:04


On Thu, Feb 16, 2012 at 3:06 PM, Robert Dailey <rcdailey_at_[hidden]> wrote:

> On Thu, Feb 16, 2012 at 5:01 PM, Jeffrey Lee Hellrung, Jr. <
> jeffrey.hellrung_at_[hidden]> wrote:
>
> > On Thu, Feb 16, 2012 at 2:40 PM, Robert Dailey <rcdailey_at_[hidden]>
> wrote:
> >
> > > I'm using C++03 and would love some way to apply move semantics to a
> > > std::string. Can boost facilitate this in some way without using smart
> > > pointers? Consider an example like below (This is a contrived example,
> so
> > > assume NRVO does not apply):
> > >
> > > std::string GetInformation( int someNumber )
> > > {
> > > // We use 'someNumber' to look up a string
> > > std::string myValue( "hello world" ); // Create the string here
> > > return myValue;
> > > }
> > >
> > > Above, the return value would result in a copy, I'd like to move it
> > > instead.
> > >
> >
> > Well I think you can always make NRVO apply if you're always returning
> the
> > same variable, which is always possible: just swap what you would
> otherwise
> > return into that common variable.
> >
> > In general, though, no, I don't think you can cleanly introduce
> (emulated)
> > move semantics non-intrusively in C++03.
>
>
> Just to be clear, what "common variable" are you referring to?
>
> Thanks for your answers.
>

std::string foo()
{
    std::string result;
    /*...*/
        // would like to "return x;", but instead...
        result.swap(x);
        return result;
    /*...*/
        // would like to "return y;", but instead...
        result.swap(y);
        return result;
}

- Jeff


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