Boost logo

Boost Users :

Subject: Re: [Boost-users] [range] What's the best way to initialize a new container from a range?
From: Szymon Gatner (szymon.gatner_at_[hidden])
Date: 2011-08-29 08:41:06


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:

template <class SinglePassRange>
boost::container::vector<typename
boost::range_value<SinglePassRange>::type> makeVector(const
SinglePassRange& rng)
{
  boost::container::vector<typename
boost::range_value<SinglePassRange>::type> v(boost::begin(rng),
boost::end(rng));

  return move(v);
}

and using it surprisingly often as move semantics allow for cheap
return-by-value

I guess you would also add template parameter for container type.

-- 
Szymon Gatner
The Lordz Games Studio
www.thelordzgamesstudio.com

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