typedef boost::tuple<int , char* , float> tup; //
tup my_tup(12, "two" , 3.4); //
boost::fusion::result_of::begin::type it(my_tup); //
std::cout << *it << '\n'; // 12
Incrementing an iterator is more difficult, but also solvable.std::cout << *boost::fusion::next( it ) << '\n'; // "two"
Sample recursion was called to help.
I would like something like:template void work(R& item)
{
//
std::cout << *item << '\n';
//
work( boost::fusion::next(item));
}
But the problem was to stop, how to understand what to stop? In principle, when the iterator becomes equal to the following construct, then the whole thing should be stopped.boost::fusion::result_of::end::type end; //
Help us call boost :: type_traits and boost :: mpl :: if_.
It would be necessary to make it so that when the iterator realizes that there is nowhere else to go, the recursion is terminated and goes the other way. As another way, let us slip a method to it - a dummy, that is, when the iterator has reached the end, it should call this method, instead of a worker. To do this, we shove both methods into the structures and place them there as static ones. Then the calls of these methods will be similar and differ only in the type of structures . And we need to change the type of structure at the last moment to stop recursion. To change the structure type, we use boost :: mpl :: if_, as a condition of which we will thrust the construction boost :: is_same from boost :: type_traits. This condition will check the current iterator with the final one each time. Go:
struct do_work
{
template<typename T,typename R >
static void work(R& item ) //
{
#pragma message("******** process tuple **************") // compile-time
// procees item
std::cout << *item << '\n';
//
typedef typename boost::fusion::result_of::end::type end; //
typedef typename boost::fusion::result_of::next::type next; //
typename next nex = boost::fusion::next( item ) ;
boost::mpl::if_<
boost::is_same<next , end>::type , // -
end_work , //
do_work //
>::type::work( nex );
}
};
struct end_work //
{
template <typename T,typename R>
static void work(R& /*tip*/) //
{
#pragma message("******** end of tuple *************")
}
};
, .
int main()
{
typedef boost::tuple<int , char* , float> tup; //
tup my_tup(12, "two" , 3.4); //
do_work::work( boost::fusion::begin(my_tup) ); //
std::cin.ignore();
return 0;
}
, , .******** process tuple **************
******** process tuple **************
******** process tuple **************
******** end of tuple *************
Everything.
Threat The author does not claim to originality and professionalism, and literacy code and generally nothing, just if I read such an article three days ago, I would save these days for my life.
haccel.pp.ru
Source: https://habr.com/ru/post/62533/
All Articles