如何修复Boost Spirit解析器的->*成员运算符编译错误?
问题解决:Boost Spirit解析器高版本Boost/GCC编译失败
问题背景
解析器在GCC 4.9.3 + Boost 1.55及更低版本可正常编译运行,但高版本环境下报以下错误:
error: no match for ‘operator->’ (operand types are ‘const _r1_type {aka const boost::phoenix::actor<boost::spirit::attribute<1>>}’ and ‘test::my_config::value_map_t {aka std::map<std::__cxx11::basic_string
, boost::variant<std::__cxx11::basic_string my_config::values, qi::_1, double, int, bool, std::vector<double, std::allocator >>>}’) _r1->
错误原因
- typedef笔误:代码中
typedef value_map_t is std::string<string, parsed_types>;是错误写法,value_map_t应该是std::map类型而非std::string - Phoenix语法兼容性变化:高版本Boost Phoenix对成员指针的绑定语法做了严格限制,不再支持
_r1->* &my_config::values这种混合actor与成员指针的写法
修复方案
1. 修正typedef定义
将错误的容器类型定义改为正确的std::map:
typedef boost::variant< std::string, double, int, bool, std::vector<double> > parsed_types; // 修正为std::map,同时统一使用std::string避免歧义 typedef std::map<std::string, parsed_types> value_map_t;
2. 调整Phoenix绑定逻辑
用嵌套的phoenix::bind先获取my_config对象的values成员,再绑定count方法,替代原有的->*写法:
one_statement = ( key > lit('=') > qi::int_ > lit(';') ) [ if_( phoenix::bind(&value_map_t::count, phoenix::bind(&my_config::values, _r1), _1) ) [ phoenix::bind(&insert_value, _r1, _1, _2) ] ];
额外注意事项
- 确保
insert_value函数的参数类型与_r1(my_config*)、_1(键类型)、_2(int值)完全匹配 - 高版本环境下建议统一使用
std::string而非简写的string,避免因命名空间导致的编译问题
内容的提问来源于stack exchange,提问作者newbieToCivilization
相关产品推荐
相关产品推荐

