mpl :: transform on boost :: fusion :: tuple

时间:2011-06-21 16:00:24

标签: c++ boost boost-mpl boost-fusion

以下代码无法使用g++ (GCC) 4.6.0 20110603 (prerelease)-std=c++0xBoost 1.46.1上进行编译。 我错过了包含还是这实际上是一个错误?如果是后者,如何解决呢?

#include <boost/mpl/vector.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/clear.hpp>

#include <boost/fusion/mpl.hpp>
#include <boost/fusion/include/clear.hpp>
#include <boost/fusion/include/clear.hpp>

#include <boost/fusion/adapted/boost_tuple.hpp>
#include <boost/fusion/include/boost_tuple.hpp>

namespace mpl = boost::mpl;

template<typename T>
struct Meta {
  T t;
  typedef mpl::vector<std::function<void(double)>, std::function<void(char)>, 
              std::function<void(int)>> MplVector;
  typedef T FusionSequence;

  //works
  typedef mpl::transform< MplVector, mpl::identity<mpl::_1> >::type result;
  //doesn't, work
  typedef typename mpl::transform< FusionSequence, mpl::identity<mpl::_1> >::type result2;
};

template<typename T>
Meta<T> make_meta(T t) { return Meta<T>({t}); }

int main()
{
  auto m = make_meta(boost::make_tuple(
               std::function<void(double)>([](double) { }),
               std::function<void(int)>([](int) { }),
               std::function<void(char)>([](char) { })));
}

2 个答案:

答案 0 :(得分:4)

如果您没有提供插入器,transform必须为您生成一个(结果必须累积某处),这实际上相当于{{1} }。 back_inserter< clear<OriginalSequence>::type >文档在这里有些误导,因为它们没有记录算法的“无插入器”版本的额外要求。

答案 1 :(得分:2)

如果使用 fusion :: make_vector 而不是make_tuple,则可以使用 mpl :: tranform 。关键的伎俩是#include <boost/fusion/include/transform.hpp>!一个简单的例子如下:

#include <boost/fusion/include/transform.hpp>
#include <boost/fusion/container/vector.hpp>
#include <boost/mpl/transform.hpp>

// Only works if <boost/fusion/include/transform.hpp> is included:
typedef boost::fusion::vector<int> FusionVector;
typedef typename boost::mpl::transform<FusionVector, boost::mpl::_1>::type FusionTransformed;