开发者

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

开发者 https://www.devze.com 2023-03-14 06:04 出处:网络
The following code does not compile on g++ (GCC) 4.6.0 20110603 (prerelease) with -std=c++0x and Boost 1.46.1.

The following code does not compile on g++ (GCC) 4.6.0 20110603 (prerelease) with -std=c++0x and Boost 1.46.1. Am I missing an include or is this actually a bug? If the latter, how to work around it?

#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) { })));
}


When you do not provide an inserter, transform has to generate one for you (the result has to be accumulated somewhere), which essentially amounts to doing something like back_inserter< clear<OriginalSequence>::type >. The transform docs are somewhat misleading here since they do not document this extra requirement for the "no-inserter" versions of the algorithm.


If you use fusion::make_vector instead of make_tuple, you could use mpl::tranform. The critical trick is to also #include <boost/fusion/include/transform.hpp>! A simple example is as follows:

#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;
0

精彩评论

暂无评论...
验证码 换一张
取 消