How to iterate over a TR1 tuple
c++, c++03, stdtuple, tuples
Solution
You could use boost::fusion if you need to call the same templated function for every object in the tuple. E.g.
template<typename T>
void invoke_operation_1(T& obj)
{
std::cout << obj << std::endl;
}
struct executor
{
template<typename T>
void operator()(T& t) const
{
invoke_operation_1(t);
}
};
typedef boost::tuple< bool
, signed char
, signed short
, signed int
, signed long long
, unsigned char
, unsigned short
, unsigned int
, unsigned long long > integral_types;
int main()
{
integral_types t(true, 0, 1, 2, 3, 4, 5, 6, 7);
boost::fusion::for_each(t, executor());
return 0;
}
Problem
Being stuck in TR1 land, for a test program I need to perform certain operations on a number of objects of specific types. I have a couple of tuple type definitions which look like this: ``` typedef std::tr1::tuple< bool , signed char , signed short , signed int , signed long long , unsigned char , unsigned short , unsigned int , unsigned long long > integral_types; ``` From each tuple type an object is to be created. I then have function templates similar to this: ``` template<typename T> void invoke_operation_1(T& obj); ``` These need to be called for all objects in a tuple object. How do I do that in C++03?