【发布时间】:2017-03-21 14:47:32
【问题描述】:
如果我们忽略这个属性,这个test::char_ 会起作用。
namespace test {
struct any_char: x3::char_parser<any_char> {
static bool const has_attribute = false;
template <typename Char, typename Context>
bool test(Char ch_, Context const&) const {
return true;
}
};
auto const char_ = any_char{};
}
但是如果我们想从test::char_ 获取属性呢?我不知道如何正确设置attribute_type,因为这个attribute_type应该是
typename std::iterator_traits<Iterator>::value_type
这在精神 qi 中不是问题,因为 qi 解析器有一个模板属性结构,我们可以从 Iterator 中获取 char 类型。
编辑:感谢 sehe 回答这个问题。自动属性传播适用于test::char_,但它仍然无法为复合属性传播正确的属性,例如+test::char_。
Edit2: 我想用这个 test::char_ 来替换 x3 中的各种 char_encoding::char_。例如我有一个简单的解析器函数 foo。输入字符串可以是 ascii 编码字符串或宽字符编码字符串。因此,我可以在任何地方使用这个 test::char_ 而不是 x3::char_ 和 x3::standard_wide::char_ 和 #IFDEF。
#ifdef NARROW_CHAR
using _char = char;
#define _STR(str) str
#else
using _char = wchar_t;
#define _STR(str) L ## str
#endif
bool foo(std::basic_string<_char> const& input, std::basic_string<_char>& attr) {
return x3::parse(
input.begin(),
input.end(),
+(test::char_ - _STR('X')),
attr);
}
Edit3: 将 attribute_type 设置为 void 或 x3::unused_type 将使 test::char_ 传播正确的属性。
namespace test {
struct any_char: x3::char_parser<any_char> {
using attribute_type = void; // or x3::unused_type
static bool const has_attribute = true;
template <typename Char, typename Context>
bool test(Char ch_, Context const&) const {
return true;
}
};
auto const char_ = any_char{};
}
Edit4: 与这个 test::char_ 相比,sehe 的 http://coliru.stacked-crooked.com/a/0a487591fbaedef4 可能是一个更好的主意。谢谢。
【问题讨论】:
-
编辑 2:我认为没有理由实现任何东西,只是别名/引用你需要的东西:coliru.stacked-crooked.com/a/0a487591fbaedef4。我可能更喜欢在单个#ifdef 中定义
spirit_encoding:coliru.stacked-crooked.com/a/0a487591fbaedef4。也就是说,我总是建议不要在这些天制作这样的精神分裂症代码库。只需在内部提交 UTF8 或 UTF16,并且仅在 I/O 需要时进行转换
标签: c++ boost boost-spirit boost-spirit-qi boost-spirit-x3