使用 std::array 作为 boost::spirit::x3 的属性

Using std::array as Attribute for boost::spirit::x3

我正在尝试使用 boost::spirit 的最新版本 x3(包含在 boost 1.54 中)将数字列表解析为固定大小的 std::array 容器。 由于 std::array 具有必要的功能,因此被检测为 Container,但缺少插入功能,因此不兼容。 这是我要完成的一个简短示例:

#include <boost/spirit/home/x3.hpp>

#include <array>
#include <iostream>
#include <string>

namespace x3 = boost::spirit::x3;
namespace ascii = boost::spirit::x3::ascii;

typedef std::array<double, 3> Vertex;

int main(int, char**) {
  using x3::double_;
  using ascii::blank;

  std::string input = "3.1415 42 23.5";
  auto iter = input.begin();

  auto vertex = x3::rule<class vertex, Vertex>{} =
    double_ >> double_ >> double_;

  Vertex v;

  bool const res = x3::phrase_parse(iter, input.end(), vertex, blank, v);
  if (!res || iter != input.end()) return EXIT_FAILURE;

  std::cout << "Match:" << std::endl;
  for (auto vi : v) std::cout << vi << std::endl;
  return EXIT_SUCCESS;
}

这不会编译,因为 std::array 没有 insert 函数。 作为解决方法,我使用了语义操作:

auto vertex() {
  using namespace x3;
  return rule<class vertex_id, Vertex>{} =
    double_[([](auto &c) { _val(c)[0] = _attr(c); })] >>
    double_[([](auto &c) { _val(c)[1] = _attr(c); })] >>
    double_[([](auto &c) { _val(c)[2] = _attr(c); })];
}

然后调用

x3::phrase_parse(iter, input.end(), vertex(), blank, v);

代替。这可行(使用 clang 3.6.0 和 -std=c++14),但我认为这个解决方案非常不优雅且难以阅读。

所以我尝试使用 BOOST_FUSION_ADAPT_ADT 将 std::array 改编为融合序列,如下所示:

BOOST_FUSION_ADAPT_ADT(
  Vertex,
  (double, double, obj[0], obj[0] = val)
  (double, double, obj[1], obj[1] = val)
  (double, double, obj[2], obj[2] = val))

然后为 Vertex 特化 x3::traits::is_container 以告诉 x3 不要将 std::array 视为容器:

namespace boost { namespace spirit { namespace x3 { namespace traits {
  template<> struct is_container<Vertex> : public mpl::false_ {};
}}}}

但这不会与 x3 一起编译。这是一个错误还是我用错了? 打电话fusion::front(v) 没有所有 x3 代码编译和工作,所以我想我的代码不是完全错误的。

不过,对于这个简单的问题,我确信有一个更简洁的 x3 解决方案,不涉及任何融合适配器或语义操作。

您发布的代码充满了草率的错误。没有理由期望其中的任何东西都能编译,真的。

无论如何,我把它清理干净了¹。当然你应该包括

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

遗憾的是,它根本不起作用。我得出的结论与(在尝试与您提到的相同的事情之后,在阅读之前:))基本相同。

这是 Spirit X3 的可用性问题 - 仍处于试验阶段。您可以在 [spirit-general] 邮件列表中报告它。反应通常很好 swift.


¹ 如果您想查看我的工作成果 http://paste.ubuntu.com/12764268/;我也使用 x3::repeat(3)[x3::double_] 进行比较。