我有一个有公共ctor和一些add()方法的类:
class object
{
object() {}
template <typename>
void add(T&& val) { // some adding here}
}
我面临的主要问题是如何采用spirit.x3列表解析器来使用object :: add()方法而不是std :: vector&lt;&gt; :: push_back?
我很容易通过简单的
实现我所需要的x3 :: int_%&#39;,&#39;
解析器(live demo)使用以下代码:
#include <boost/spirit/home/x3.hpp>
#include <iostream>
#include <vector>
namespace x3 = boost::spirit::x3;
namespace parse_to_object
{
struct object
{
using value_type = int;
object() { std::cout << "object::object() - invoked" << std::endl; }
void add(value_type val) { _data.push_back(val); }
std::vector<value_type> _data;
};
const x3::rule<struct Test, object> r_ints("r_ints");
const auto r_ints_def = x3::int_ % ',';
BOOST_SPIRIT_DEFINE(r_ints);
}
namespace boost { namespace spirit { namespace x3 { namespace traits {
template<>
struct push_back_container<parse_to_object::object>
{
template<typename T>
static bool call(parse_to_object::object& obj, T&& val)
{
obj.add(std::move(val));
return true;
}
};
}}}}
int main()
{
const std::string text("1,2,3,4");
auto begin = std::begin(text);
const auto end = std::end(text);
parse_to_object::object result;
const auto ok = x3::phrase_parse(begin, end, parse_to_object::r_ints, x3::space, result);
std::cout << "ok = " << std::boolalpha << (ok && begin == end) << std::endl;
std::copy(result._data.begin(), result._data.end(), std::ostream_iterator<int>(std::cout, " "));
return 0;
}
但不幸的是,当我尝试更多的编译示例时,如
&#39; {&#39; &GT;&GT; x3 :: int_&gt;&gt; &#39;:&#39; &GT;&GT; x3 :: int_&gt;&gt; &#39;}&#39;)%&#39;,&#39;
我收到编译错误(live demo):
/opt/wandbox/boost-1.67.0/clang-head/include/boost/spirit/home/x3/support/traits/container_traits.hpp:102:45:错误:没有命名的类型&#39; iterator& #39; in&#39; parse_to_object :: object&#39; :mpl :: identity {};
有人可以协助使用spirit.x3特性,并举例说明如何使用自定义类代替std :: vector&lt;&gt;列表解析器?
答案 0 :(得分:0)
最后,它失踪了包括:
<?php
$travels = array(
'0' => array('Départ ' =>'Paris', 'Arrivée ' => 'Nantes', 'Heure de départ ' => '11:00', 'Heure d\'arrivée ' => '12:34', 'Chauffeur ' => 'Thomas'),
'1' => array('Départ ' =>'Orléans', 'Arrivée ' => 'Nantes', 'Heure de départ ' =>'05:15', 'Heure d\'arrivée ' => '09:32', 'Chauffeur ' =>'Mathieu'),
'2' => array('Départ ' =>'Dublin', 'Arrivée ' => 'Tours', 'Heure de départ ' =>'07:23', 'Heure d\'arrivée ' => '08:50', 'Chauffeur ' =>'Nathanaël'),
'3' => array('Départ ' =>'Paris', 'Arrivée ' => 'Orléans', 'Heure de départ ' =>'03:00', 'Heure d\'arrivée ' => '05:26', 'Chauffeur ' => 'Clément'),
'4' => array('Départ ' =>'Paris', 'Arrivée ' => 'Nice', 'Heure de départ ' => '10:00', 'Heure d\'arrivée ' => '12:09', 'Chauffeur ' => 'Audrey'),
'5' => array('Départ ' =>'Nice', 'Arrivée ' => 'Nantes', 'Heure de départ ' => '10:40', 'Heure d\'arrivée ' =>'13:00', 'Chauffeur ' =>'Pollux'),
'6' => array('Départ ' =>'Nice', 'Arrivée ' => 'Tours', 'Heure de départ ' => '11:00', 'Heure d\'arrivée ' => '16:10', 'Chauffeur ' =>'Edouard'),
'7' => array('Départ ' =>'Tours', 'Arrivée ' => 'Amboise', 'Heure de départ ' => '16:00', 'Heure d\'arrivée ' => '18:40', 'Chauffeur ' =>'Priscilla'),
'8' => array('Départ ' =>'Nice', 'Arrivée ' => 'Nantes', 'Heure de départ ' => '12:00', 'Heure d\'arrivée ' =>'16:00', 'Chauffeur ' =>'Charlotte')
);
$selected = 'Paris';
function get_destination($travel) {
global $selected;
return $travel["Départ "] === $selected;
}
print_r(array_filter($travels, 'get_destination'));
#include <boost/fusion/adapted/std_pair.hpp>
默认情况下不适用。
附注:
std::pair
应为std::move
,其中包含&#34;通用引用&#34; (或完美转发)
<强> Live On Coliru 强>
std::forward<T>
打印
#define BOOST_SPIRIT_X3_DEBUG
#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/spirit/home/x3.hpp>
#include <iostream>
#include <vector>
namespace x3 = boost::spirit::x3;
namespace parse_to_object
{
struct object
{
using value_type = std::pair<int,int>;
object() { std::cout << "object::object() - invoked" << std::endl; }
void add(value_type val) { _data.push_back(std::move(val)); }
std::vector<std::pair<int,int>> _data;
};
const x3::rule<struct Test, object> r_ints("r_ints");
const auto r_ints_def = ('{' >> x3::int_ >> ':' >> x3::int_ >> '}') % ',';
BOOST_SPIRIT_DEFINE(r_ints)
}
namespace boost { namespace spirit { namespace x3 { namespace traits {
template<> struct push_back_container<parse_to_object::object>
{
template<typename T>
static bool call(parse_to_object::object& obj, T&& val)
{
obj.add(std::forward<T>(val));
return true;
}
};
}}}}
int main()
{
const std::string text("{1:2},{3:4}");
auto begin = std::begin(text), end = std::end(text);
parse_to_object::object result;
auto ok = phrase_parse(begin, end, parse_to_object::r_ints >> x3::eoi, x3::space, result);
std::cout << "ok = " << std::boolalpha << ok << "\n";
for (auto& p : result._data)
std::cout << "{" << p.first << ", " << p.second << "} ";
std::cout << "\n";
}