有比手动编写这样的实用程序更好的解决方案吗?
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 ) );
}
像这样使用:
std::tuple<char, int, double> tt;
TupleForEach( tt, (boost::lambda::_1 = 5) );