BOOST_FUSION_ADAPT_STRUCT のイテレータは複数作れない2020年03月09日 01時39分09秒

BOOST_FUSION_ADAPT_STRUCT のイテレータのアクセス順は定義時に決定されるのを確認した。

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'
BOOST_FUSION_ADAPT_STRUCT(
^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/boost/fusion/adapted/struct/adapt_struct.hpp:68:9: note: expa
nded from macro 'BOOST_FUSION_ADAPT_STRUCT'
        BOOST_FUSION_ADAPT_STRUCT_BASE(                                         \
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/boost/fusion/adapted/struct/detail/adapt_base.hpp:245:13: not
e: expanded from macro 'BOOST_FUSION_ADAPT_STRUCT_BASE'
            BOOST_FUSION_ADAPT_STRUCT_TAG_OF_SPECIALIZATION(
構造体の名前のみを使っているようで、再定義されているとエラーが出た。

前回次回