[英]Boost Spirit semantic actions on non-Fusion adapted structs
假如我有一個簡單的結構:
struct huh { char xxx; };
在沒有使用Boost Fusion改編的結構的情況下,我想在Spirit規則的語義動作中找到一個更簡單的代理來操作成員變量。 現在,這當然不起作用:
_val.xxx = _1
但是我希望得到一個比與鳳凰綁定更清潔的東西:
bind(&huh::xxx, _val) = _1
這是一個簡單圖片的工作示例,它編譯了一個clang和一個g ++:
#define BOOST_SPIRIT_USE_PHOENIX_V3 1
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/phoenix/bind/bind_member_variable.hpp>
struct huh { char xxx; };
int main() {
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phx = boost::phoenix;
qi::rule<std::string::const_iterator, huh(), ascii::space_type> start;
start = qi::char_[
phx::bind(&huh::xxx, qi::_val) = qi::_1
];
return 0;
}
有任何想法嗎?
與融合結合正是使它“比與鳳凰結合更清潔 ”的粘合劑。
那么,有你的解決方案。 或者,考慮您的表達模板,可能:
auto _xxx = phx::bind(&huh::xxx, _val);
所以你可以寫:
start = char_[ _xxx = _1 ];
或者,使用函數或自定義點:
添加特征
namespace boost { namespace spirit { namespace traits {
template <> struct assign_to_attribute_from_value<huh, char, void> {
static void call(char c, huh& attr) { attr.xxx = c; }
};
} } }
你現在可以簡單地寫
qi::rule<std::string::const_iterator, huh(), ascii::space_type> start;
start = qi::char_;
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/spirit/include/qi.hpp>
struct huh { char xxx; };
namespace boost { namespace spirit { namespace traits {
template <> struct assign_to_attribute_from_value<huh, char, void> {
static void call(char c, huh& attr) { attr.xxx = c; }
};
} } }
int main() {
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
qi::rule<std::string::const_iterator, huh(), ascii::space_type> start;
start = qi::char_;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.