Boost logo

Boost Users :

Subject: Re: [Boost-users] [range] What's the best way to initialize a new container from a range?
From: Martin B. (0xCDCDCDCD_at_[hidden])
Date: 2011-08-29 11:38:43


On 29.08.2011 14:41, Szymon Gatner wrote:
> 2011/8/29 Martin B.<0xCDCDCDCD_at_[hidden]>:
>> On 26.08.2011 20:21, Ovanes Markarian wrote:
>>>
>>> On Fri, Aug 26, 2011 at 2:30 PM, Martin B.<0xCDCDCDCD_at_[hidden]
>>> <mailto:0xCDCDCDCD_at_[hidden]>> wrote:
>>>
>>> Hi!
>>>
>>> Say I have a range R and I want to construct a new container from
>>> the range R. Will I always have to repeat the expression yielding
>>> the range, or is there a shorter way?
>>>
>
> I do something like this:
>
> ...
> {
> ... v(boost::begin(rng), boost::end(rng));
>
> return move(v);
> }
>
> and using it surprisingly often as move semantics allow for cheap
> return-by-value
>

That code is wrong. (As far as I understand C++11.)
AFAIK, you should *never*(?) explicitly move the return value as:

* The compiler is free to do so anyway. (AFAIK)
* It will prevent RVO. (AFAIK)

so I would just leave the move and do a `return v;` relying on the fact
that even my C++03 my compiler will do RVO and in-place construction of
the returned vector anyway.

cheers,
Martin


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net