Boost logo

Boost Users :

Subject: Re: [Boost-users] Ranges to Collections
From: John M. Dlugosz (mpbecey7gu_at_[hidden])
Date: 2011-06-07 10:25:51


On 6/6/2011 9:31 PM, ÇÇ־ǿ qiaozhiqiang-at-leadcoretech.com wrote:
> Maybe you want make_vector() , make_list().
>
> template<class T, class SinglePassRange>
> T make_container(const SinglePassRange& from)
> {
> T c;
> boost::push_back(c, from);
> return move(c);
> }

I think it would be better to make use of the two-iterator form of the constructor rather
than calling push_back. That way it can allocate storage for all elements in advance if
the kind of range allows for finding the difference easily.

{
return T (from.begin(), from.end());
}

I guess your way optimizes for the case where it needs to slowly traverse the range in
order to find the end() first. I suppose boost::push_back does something similar if it
can, but it still constructs as zero size and then resizes.

> template<class SinglePassRange>
> std::vector<typename SinglePassRange::value_type> make_vector(const SinglePassRange& from)
> {
> return make_container<std::vector<typename SinglePassRange::value_type>>(from);
> }

The make_container needs to be told the exact container type in the call, and your
make_vector et al. is a different name for each possible container. I would think you
could use a template template argument and pass in the container species only, and have it
obtain the value_type for itself. I think that would be good enough, as the details of the
value types is the hard part.

But, I speculate that a simple expression could figure out the complete type by itself via
using a templatized conversion operator.


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