c++ - How to iterate through std::tuple? -


this question has answer here:

any better solution manually writing utility 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 used this:

std::tuple<char, int, double> tt; tupleforeach( tt, (boost::lambda::_1 = 5) ); 

even though there several answers provided in previous, related question (and 1 provide yourself), initial impression need iterate on tuple may reflection of poor design.

as know, reason why cannot iterate on std::tuple using standard c++ algorithms because std::tuple not fulfill container concept. and, precisely, not fulfill such concept because std::tuples not have value_type (they heterogeneous). know used tuple because did not want create own polymorphic type , store in standard container (e.g., std::vector<std::shared_ptr<baseclass>>). gave quick gain. means voluntarily gave advantages of containers.

it may work, somehow feels forced , unnatural: if need container semantics, why not use container? if need polymorphic semantics, why not use polymorphic type?

probably i'm exaggerating, initial impression.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -