提升精神:内置终端应该使用什么类型名称

boost spirit: What type names should be used for the built in terminals?

本文关键字:什么 类型 终端 内置      更新时间:2023-10-16

我正在重构我现有的一个类型系统(类型模型),它使用 spirit 进行字符串序列化。我正在使用类型特征的编译时建模结构。

template<>
type_traits<int4_type>
{
    typedef boost::spirit::qi::int_parser<boost::int32_t> string_parser;
}  
template<>
type_traits<string_type>
{
    typedef boost::spirit::ascii::string string_parser;
}

在这个例子中,我展示了原始解析器,但我也希望加入规则。

int4 类型有效,但这是因为 (home/qi/numeric/int.hpp +27):

namespace tag
    {
        template <typename T, unsigned Radix, unsigned MinDigits
                , int MaxDigits> 
        struct int_parser {};
    }
    namespace qi
    {
        ///////////////////////////////////////////////////////////////////////
        // This one is the class that the user can instantiate directly in 
        // order to create a customized int parser
        template <typename T = int, unsigned Radix = 10, unsigned MinDigits = 1
                , int MaxDigits = -1>
        struct int_parser
          : spirit::terminal<tag::int_parser<T, Radix, MinDigits, MaxDigits> > 
        {};
    }

字符串 Typedef 不起作用,EPS 也不起作用。我无法弄清楚引用字符串解析器的原因。但是,在eps的情况下,它归结为:

#define BOOST_SPIRIT_TERMINAL(name)                                             
    namespace tag { struct name {};  }                                          
    typedef boost::proto::terminal<tag::name>::type name##_type;                
    name##_type const name = {{}};                                              
    inline void silence_unused_warnings__##name() { (void) name; }              
    /***/

这意味着我无法键入它,它是一个原型终端构造,或者说得不透明,一个 const 全局定义。

我的问题:如何键入定义规则,语法,原始解析器?

注意:我已经开始努力给我所有的"类型"一个封装规则的函子,然后使其成为类型特征。

我不确定它是否在所有情况下都有效,但我对 Skipper 所做的是使用 BOOST_TYPEOF :

typedef BOOST_TYPEOF(boost::spirit::ascii::space - (boost::spirit::qi::eol | boost::spirit::qi::eoi)) SkipperT;

所以你的例子将是

typedef BOOST_TYPEOF(boost::spirit::ascii::string) string_parser;

可能有一种更好/更优雅的方式,但它目前适用于我需要的。