Boost logo

Boost :

Subject: Re: [boost] Review Request : Boost.Range Extension
From: Michel Morin (mimomorin_at_[hidden])
Date: 2012-06-06 11:33:13


`as_container` looks interesting!

Here are some comments on `as_container`:
* `as_container_functor` and `operator |` live in different name spaces.
So ADL does not work.

* `as_container` should live in boost::adaptors?

* Built-in arrays are not supported by `as_container`.

* Template conversion operator ambiguity:
In C++03 and C++11, the following code
    boost::array<int, 3> ar = {{1, 2, 3}};
    std::vector<int> v(ar | as_container);
fails to compile due to the ambiguity between
  - `explicit vector(const allocator_type&)`,
  - `vector(initializer_list<value_type>)`, and
  - `vector(const vector&)`.

In C++11, the following code
    boost::array<int, 3> ar = {{1, 2, 3}};
    std::vector<int> v;
    v = ar | as_container;
fails to compile due to the ambiguity between
  - `vector& operator=(initializer_list<value_type>)`, and
  - `vector& operator=(const vector&)`.

One solution to this problem is applying SFINAE by "default template arguments
for function templates" to the template conversion operator:
    template <
        class Container
      , class Dummy1 = typename Container::difference_type
      , class Dummy2 = typename Container::iterator
>
    operator Container() const { /* ... */ }
(`Dummy1` is used to SFINAE out `std::initializer_list` and `Dummy2` is
used to SFINAE out `std::allocator`.)
But this is a C++11 feature and so ambiguity with `std::allocator` is not
solved in C++03. If there is no simple solution, simply documenting the
limitation is an acceptable solution, I think.

Regards,
Michel


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