我試圖創建一個簡單的解析器,需要使用boost::spirit::x3
兩種可能的角色之一。問題是x3::char_('#') | x3::char_('.')
似乎有一個boost::variant<char, ?>
類型的屬性。這意味着我必須在_attr
上使用boost::get<char>
,而應該直接轉換爲char
。提振精神X3(A | A)屬性類型是變種<A, ?>,而不是一個
http://ciere.com/cppnow15/x3_docs/spirit/quick_reference/compound_attribute_rules.html,它說:A | A -> A
如果mapChars
註釋掉版本則使用它轉化爲char
,但不是|
。
我在升壓版本1.63.0和Linux上。代碼無法在g ++和clang ++中使用-std=c++14
進行編譯。
我在做什麼錯?
#include <iostream>
#include <string>
#include <boost/spirit/home/x3.hpp>
int main() {
std::string s("#");
namespace x3 = boost::spirit::x3;
auto f = [](auto & ctx) {
auto & attr = x3::_attr(ctx);
//char c = attr; // doesn't work
char c = boost::get<char>(attr); // does work
};
auto mapChar = x3::char_('#') | x3::char_('.'); // _attr not convertible to char, is a variant
//auto mapChar = x3::char_('#'); // _attr convertible to char, isn't a variant
auto p = mapChar[f];
auto b = s.begin();
bool success = x3::parse(b, s.end(), p);
std::cout << "Success: " << success << ' ' << (b == s.end()) << '\n';
}
我想'X3 ::字符_( 「#」)'應該做你想要什麼。當然這並不能解決'A | A->變體'的問題。 – llonesmiz
對於這個示例,這是非常相當的 – sehe