BOOST_FUSION_ADAPT_STRUCT は namespace 内で使えない2020年03月07日 11時49分11秒

BOOST_FUSION_ADAPT_STRUCT を使って、構造体の変数をイテレータを使ってアクセスできて凄いのだが、これにも欠点はある。

実は、このマクロは名前空間ないで使えない。実験や例などではわざわざ namespace を使わないでやるので、大丈夫。そして、実際に使おうとして、大きめのコードを書いてから大量のエラーに見舞われた。

これがエラーの出力。

% c++ -std=c++11 -I /usr/local/include boost_fusion_adapt_struct2.cpp
boost_fusion_adapt_struct2.cpp:23:1: error: explicit specialization of
      non-template struct 'tag_of'
BOOST_FUSION_ADAPT_STRUCT(
^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/boost/fusion/adapted/struct/adapt_struct.hpp:68:9: note: 
      expanded from macro 'BOOST_FUSION_ADAPT_STRUCT'
  ...BOOST_FUSION_ADAPT_STRUCT_BASE(                                         \
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/boost/fusion/adapted/struct/detail/adapt_base.hpp:245:13: note: 
      expanded from macro 'BOOST_FUSION_ADAPT_STRUCT_BASE'
  ...BOOST_FUSION_ADAPT_STRUCT_TAG_OF_SPECIALIZATION(                    \
これは前回のコードに単純に namespace を付けただけ。
#include <string>
#include <iostream>
#include <boost/fusion/algorithm.hpp>

#include <boost/fusion/include/adapt_struct.hpp>

namespace member
{
 
struct person
{
    std::string name_;
    int age_;
 
    person( const std::string& name, int age )
        : name_( name )
        , age_( age )
    {
    }
};
 
 
BOOST_FUSION_ADAPT_STRUCT(
    member::person
    , ( std::string, name_ )
    , ( int, age_ )
)
 
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() );
}

対処方法は BOOST_FUSION_ADAPT_STRUCT を名前空間の外に出すこと。

#include <string>
#include <iostream>
#include <boost/fusion/algorithm.hpp>

#include <boost/fusion/include/adapt_struct.hpp>

namespace member
{

struct person
{
    std::string name_;
    int age_;

    person( const std::string& name, int age )
        : name_( name )
        , age_( age )
    {
    }
};

}

BOOST_FUSION_ADAPT_STRUCT(
    member::person
    , ( std::string, name_ )
    , ( int, age_ )
)

namespace work
{

struct print
{
    template< typename T >
    void operator()( const T& t ) const
    {
        std::cout << t << std::endl;
    }
};

}

int main()
{
    member::person p( "Boost User", 10 );
    boost::fusion::for_each( p, work::print() );
}
これでコンパイル出来る。
% c++ -std=c++11 -I /usr/local/include boost_fusion_adapt_struct2.cpp
% ./a.out 
Boost User
10

前回次回