7

Any better solution than manually writing a utility like this?

template < size_t > struct SizeT { };

template < typename TupleType, typename ActionType >
inline void TupleForEach( TupleType& tuple, ActionType action )
{
    TupleForEach( tuple, action, SizeT<std::tuple_size<TupleType>::value>() );
}

template < typename TupleType, typename ActionType >
inline void TupleForEach( TupleType& tuple, ActionType action, SizeT<0> ) { }

template < typename TupleType, typename ActionType, size_t N >
inline void TupleForEach( TupleType& tuple, ActionType action, SizeT<N> )
{
    TupleForEach( tuple, action, SizeT<N-1>() );
    action( std::get<N-1>( tuple ) );
}

To be used like this:

std::tuple<char, int, double> tt;
TupleForEach( tt, (boost::lambda::_1 = 5) );
Praetorian
  • 106,671
  • 19
  • 240
  • 328
Vahagn
  • 4,670
  • 9
  • 43
  • 72
  • 1
    What are your criteria for "better"? – Casey Aug 09 '13 at 21:07
  • @Casey -- A library implementation in `std` or `boost`; if not, then an implementation with less code then this; or maybe there is an issue in this implementation. – Vahagn Aug 10 '13 at 07:03

1 Answers1

1

Even though there are several answers provided in a previous, related question (and the one you provide yourself), my initial impression is that the need to iterate over the tuple may be a reflection of a poor design.

As you know, the reason why we cannot iterate over a std::tuple using standard C++ algorithms is because std::tuple does not fulfill the Container concept. And, precisely, it does not fulfill such concept because std::tuples do not have a value_type (they are heterogeneous). I know that you used a tuple because you did not want to create your own polymorphic type and store it in an standard container (e.g., std::vector<std::shared_ptr<BaseClass>>). This gave you a quick gain. But that also means that you voluntarily gave up the advantages of Containers.

It may work, but it somehow feels forced and unnatural: if you need container semantics, why not use a container? If you need polymorphic semantics, why not use a polymorphic type?

Probably I'm exaggerating, but this is my initial impression.

Community
  • 1
  • 1
Escualo
  • 40,844
  • 23
  • 87
  • 135
  • The reason why I did't created a polymorphic base class for types in the tuple isn't a whim :) These types are from kind of third-party, so who uses them, for sure, is not necessarily the one who maintains them. These types, however, share a common concept, thus some operations are valid for each of them, so the one who stores a bunch of objects of these types, might want to perform such operations over that objects. – Vahagn Aug 10 '13 at 07:43
  • 4
    Moreover, consider a tuple of `char`, `int` and `double`. Or a tuple of some pointers. In the first case there is e.g. a valid operation of setting the tuple members to zero, and in the second case there is e.g. a valid operation of setting the tuple members to `nullptr`, but for sure in C++ it is a kind of nonsense to consider a base class for all arithmetic types or a base class for all pointer types. – Vahagn Aug 10 '13 at 07:50