使用增强融合显示一个扁平的凤凰表情

Displaying a Flattened Phoenix Expression using Boost Fusion

本文关键字:一个 凤凰 增强 融合 显示      更新时间:2023-10-16

根据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定义。我还想用提高凤凰;然而,如果我在fusion::for_each的调用中使用boost::phoenix::arg_names中定义的_1版本,我得到一个错误:不能将' std::ostream{又名std::basic_ostream} '左值绑定到' std::basic_ostream&& ' 。我可以像这样在融合变换中使用凤凰占位符吗?

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()
  );
}