Boost logo

Boost :

Subject: Re: [boost] [foreach][assign] Does foreach work with assign::list_of?
From: Michel MORIN (mimomorin_at_[hidden])
Date: 2011-06-13 03:56:04

MaximYanchenko wrote:
> This simple example doesn't compile with GCC 4.4 + Boost 1.46.1:
> #include <boost/assign/list_of.hpp>
> #include <boost/foreach.hpp>
> #include <iostream>
> int main()
> {
>  BOOST_FOREACH( int i, boost::assign::list_of(3)(1)(4)(1)(5) )
>    std::cout << i << ' ';
> }
> error: operands to ?: have different types 'const
> boost::foreach_detail_::rvalue_probe<boost::assign_detail::generic_list<int> >'
> and 'boost::assign_detail::generic_list<int>'
> I believe both Foreach and Assign library are to ease sequence
> creation/iteration on the fly, so they should work together smoothly.

The problem is that `generic_list<T>` (a return value of `assign::list_of()`)
has very generic conversion operator:
    template<class Container> operator Container() const;

This generic conversion operator leads to the compiler error;
Containers used with `BOOST_FOREACH` should not be convertible to
`rvalue_probe`. In the above case, `generic_list<T>` is convertible to
`rvalue_probe< generic_list<T> >` and the compiler error happens.

If SFINAE could be used with conversion operators, we could selectively
disable problematic conversions. Unfortunately, we cannot apply SFINAE
to conversion operators in C++03.


Boost list run by bdawes at, gregod at, cpdaniel at, john at