Boost logo

Boost Users :

From: Germán Diago Gómez (germandiago_at_[hidden])
Date: 2008-05-06 07:23:01


Hello. I'm trying to compile this code, but it doesn't do the trick and
I don't know why.

struct print
{
        typedef void result_type;

        template <class T>
        void operator()(const T & elem) const
        {
                std::cout << elem << std::endl;
        }
};

template <class Tuple, class UnaryF, int i = 0>
struct tuple_for_each_helper
{
private:
        Tuple & t_;
        UnaryF f_;
public:
        typedef typename UnaryF::result_type result_type;

        tuple_for_each_helper(Tuple & t, UnaryF f) : t_(t), f_(f)
        {
        }

        result_type operator()(typename
boost::enable_if<is_same<result_of<UnaryF (const Tuple &)>, void>
>::type * = 0)
        {
                f_(get<i>(t_));
                typename boost::mpl::if_c<i == tuple_size<Tuple>::value - 1,
                        tuple_for_each_helper<Tuple, UnaryF, -1>,
                        tuple_for_each_helper<Tuple, UnaryF, i + 1> >::type(t_, f_)();
        }
};

template <class Tuple, class UnaryF>
struct tuple_for_each_helper<Tuple, UnaryF, -1>
{
        typedef typename UnaryF::result_type result_type;

        tuple_for_each_helper(Tuple & t, UnaryF f) {}
        result_type operator()() {}
};

template <class Tuple, class UnaryF>
typename
result_of<UnaryF (const Tuple &)>::type tuple_for_each (Tuple & t,
UnaryF f,
typename boost::enable_if<is_same<result_of<UnaryF (const Tuple &)>,
void> >::type * = 0)
{
    tuple_for_each_helper<Tuple, UnaryF>(t, f)();
}

int main(int argc, char * argv[])
{
        tuple<int, float, string, string> t(3, 5.3, "hello", "goodbye");
        tuple_for_each(t, print());
}

And the error is:

tuple_each.cpp: In function ‘int main(int, char**)’:
tuple_each.cpp:99: error: no matching function for call to
‘tuple_for_each(std::tr1::tuple<int, float, std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, std::tr1::_NullClass,
std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass,
std::tr1::_NullClass, std::tr1::_NullClass>&, print)’

If I change boost::enable_if by boost::disable_if the code compiles. But
if I check typeid(typename print::result_type).name() and
typeid(result_of<UnaryF (const Tuple &)>::type).name() they are the
same. I'm using g++ 4.2.3 and boost 1.34.1.

Thanks in advance.


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