c ++错误访问boost融合对中的字段类型

时间:2014-03-14 15:12:12

标签: c++ boost template-meta-programming

我尝试从融合地图创建filter_view,但似乎无法使模板元功能起作用。

所以我有一个融合地图,其结构类似于

#include <boost/fusion/container/map.hpp>
#include <boost/fusion/view/filter_view.hpp>

template <typename> struct SetField { };
using IDType = int;
using NameType = std::string;
using TimeField = double;

using namespace boost::fusion;
typedef map<pair<struct ActId,  SetField<IDType> >,
            pair<struct ActName, SetField<NameType> >,
            pair<struct ActStart, TimeField>,
            pair<struct ActEnd,   TimeField> > ActivityMapType;

我想创建一个filter_view,排除第二个类型是TimeField

的字段
int main()
{
    ActivityMapType map_;
    filter_view<
            ActivityMapType const, 
            boost::mpl::not_<boost::is_same<result_of::second<boost::mpl::_>::type, TimeField>>
        > view(map_);
}

我得到的编译错误是

 /home/mike/boost/boost/fusion/support/pair.hpp: In instantiation of
             'struct boost::fusion::result_of::second<mpl_::arg<-1> >':
 /home/mike/project/Filter/FusionActivityFilter.h:328:55:   required
 from here /home/mike/boost/boost/fusion/support/pair.hpp:68:48: error:
 no type named 'second_type' in 'struct mpl_::arg<-1>'
              typedef typename Pair::second_type type;

我想知道我是不是错误地使用了boost :: mpl :: _占位符,我是这个元编程的新手。

1 个答案:

答案 0 :(得分:1)

我找到了一个解决方案,基本上是here

给出的答案
template <typename FPair>
struct get_second 
{
   typedef typename FPair::second_type type;
};

int main()
{
    ActivityMapType map_;
    typedef boost::fusion::result_of::filter_if<
                       boost::mpl::lambda<
                               boost::mpl::not_<
                                        boost::is_same<
                                                  TimeField,
                                                  get_second<boost::mpl::_1>
                                        >
                               >
                       >::type
            >::type no_time_type;
    no_time_type noTimes(map_);
}

我不太明白为什么我不能用boost :: fusion :: filter_view做同样的事情。我以为我找到了一张看似潜在原因的加油票,但我现在找不到了。似乎filter_view阻止了内部元函数看到占位符_1的实际类型。