BOOST_FUSION_ADAPT_STRUCT のイテレータは複数作れない ― 2020年03月09日 01時39分09秒
map や list 等のコンテナ系は実装によってその順番が決まっている。map はソートされ、list は入れた順列になる。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
, ( std::string, name_ )
, ( int, 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++ -std=c++11 -I /usr/local/include/ boost_fusion_adapt_struct.cpp
boost_fusion_adapt_struct.cpp:25:1: error: redefinition of'tag_of
最近のコメント