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
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, void>
::type * = 0)
{
f_(get<i>(t_));
typename boost::mpl::if_c,
tuple_for_each_helper >::type(t_, f_)();
}
};
template
struct tuple_for_each_helper
{
typedef typename UnaryF::result_type result_type;
tuple_for_each_helper(Tuple & t, UnaryF f) {}
result_type operator()() {}
};
template
typename
result_of::type tuple_for_each (Tuple & t,
UnaryF f,
typename boost::enable_if,
void> >::type * = 0)
{
tuple_for_each_helper(t, f)();
}
int main(int argc, char * argv[])
{
tuple 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, std::basic_string, 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::type).name() they are the
same. I'm using g++ 4.2.3 and boost 1.34.1.
Thanks in advance.