遵循《 Proto用户指南》的Expressions as Fusion Sequences部分,我要遍历扁平化的proto表达式:_1 + 2 + 3 + 4:

#include <iostream>
#include <boost/phoenix.hpp>

namespace proto   = boost::proto;
namespace fusion  = boost::fusion;
namespace phoenix = boost::phoenix;

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

boost::proto::terminal<int>::type const _1 = {1};

int main(int argc, char *argv[])
{
  fusion::for_each(
    fusion::transform(
      proto::flatten(_1 + 2 + 3 + 4)
    , proto::functional::value()
    )
  , display()
  );
  return 0;
}

如上所示,使用_1定义了proto::terminal占位符。我也想用
助力凤凰但是,如果我改为在对_1的调用中使用boost::phoenix::arg_names中定义的fusion::for_each版本,则会收到错误:无法将“std::ostream {aka std::basic_ostream}”左值绑定(bind)到“std::basic_ostream &&” 。我可以在融合转换中使用类似Phoenix的占位符吗?

最佳答案

没有用于phoenix::arg_names::_1的ostream插入器。不过,我们可以轻松添加一个。这为我用clang++(trunk)编译:

#include <iostream>
#include <boost/phoenix.hpp>

namespace proto   = boost::proto;
namespace fusion  = boost::fusion;
namespace phoenix = boost::phoenix;

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

namespace boost { namespace phoenix
{
  template<int N>
  std::ostream& operator<<(std::ostream& sout, argument<N> const& arg)
  {
    return sout << "_" << N;
  }
}}

int main()
{
  fusion::for_each(
    fusion::transform(
      proto::flatten(phoenix::arg_names::_1 + 2 + 3 + 4)
    , proto::functional::value()
    )
  , display()
  );
}

10-06 05:24
查看更多