BOOST_FUSION_ADAPT_STRUCT を使って、構造体の変数をイテレータを使ってアクセスした。BOOST_FUSION_ADAPT_STRUCT 内に並べる順序を変えると、イテレータとしての順序も変わる。
#include <string>
#include <iostream>
#include <boost/fusion/algorithm.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
struct person
{
std::string name_;
int age_;
person( const std::string& name, int age )
: name_( name )
, age_( age )
{
}
};
BOOST_FUSION_ADAPT_STRUCT(
person
, ( int, age_ )
, ( std::string, name_ )
)
struct print
{
template< typename T >
void operator()( const T& t ) const
{
std::cout << t << std::endl;
}
};
int main()
{
person p( "Boost User", 10 );
boost::fusion::for_each( p, print() );
}
該当部分のコードは短いが、全文載せた。
% c++ -I /usr/local/include/ boost_fusion_adapt_struct.cpp
% ./a.out
10
Boost User
そして、実行結果。
前回。次回。
最近のコメント