我正在寻找实现variadic函数的最简单方法,它采用boost :: spirit :: qi规则列表并将列表扩展为格式表达式:rule1 | rule2 | rule3 | ....让我们假设规则不合成任何属性.非常感谢您的帮助.
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <string>
#include <iostream>
#include <boost/spirit/include/phoenix_operator.hpp>
namespace qi = boost::spirit::qi;
namespace ph = boost::phoenix;
namespace ascii = boost::spirit::ascii;
using boost::spirit::qi::phrase_parse;
using boost::spirit::qi::ascii::space;
using boost::spirit::iso8859_1::char_;
typedef qi::rule<std::string::const_iterator,ascii::space_type> mrule_t;
typedef qi::rule< std::string::const_iterator,std::string() > wrule_t;
Run Code Online (Sandbox Code Playgroud)
//How to deduce expandBitwise() return type ?
template<typename T>
T expandBitwise(T& t)
{
return t.rule_;
}
template<typename T,typename ...Tail>
T expandBitwise(T& t,Tail& ...tail)
{
return t.rule_ | expandBitwise(tail...);
}
struct TStruct
{
mrule_t …Run Code Online (Sandbox Code Playgroud) 我的目标是"动态"构建由简单的置换表达式组成的表达式,但我无法使其完全正常运行.即这两个序列表明它不能按预期工作:
-b btoken -c ctoken -d dtoken -a atoken
-c ctoken -d dtoken -a atoken -b btoken
最终目标是能够动态构建可以解析不同类型的表达式:int,float,double ...您的建议表示赞赏:-)
#define BOOST_SPIRIT_DEBUG
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <iostream>
#include <iomanip>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
namespace ph = boost::phoenix;
namespace ascii = boost::spirit::ascii;
typedef qi::rule<std::string::const_iterator,ascii::space_type> mrule_t;
typedef qi::rule<std::string::const_iterator,std::string() > wrule_t;
struct TStruct
{
mrule_t rule_;
template<typename T,typename R>
TStruct( T& rVar,const std::string&name, const R& rule ) :
rule_( qi::lit(name) >> rule[ ph::ref(rVar) = qi::_1 ] )
{
rule_.name(name);
} …Run Code Online (Sandbox Code Playgroud)