c++ - 振奋 spirit : What type names should be used for the built in terminals?

标签 c++ boost metaprogramming boost-spirit tmp

我正在重构一个类型系统(类型模型),它使用 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; }              \
    /***/

这意味着我不能对它进行类型定义,它是一个原型(prototype)终端构造,或者说不透明,是一个 const 全局定义。

我的问题:如何对规则、语法、原始解析器进行类型定义?

注意:我已经开始着手为我所有的“类型”提供一个封装规则的仿函数,然后将其作为类型特征。

最佳答案

我不确定它是否适用于所有情况,但我对 Skipper 进行 typedef 的操作是使用 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;

可能有更好/更优雅的方式,但它目前可以满足我的需要。

关于c++ - 振奋 spirit : What type names should be used for the built in terminals?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6023707/

相关文章:

c++ - boost 单元测试主要功能?

c++ - 当我使用 bcp 导出 boost 线程时如何克服构建错误?

ios - 在闭包中使用元类型

ruby - "public"、 "private"和 "protected"方法究竟做了什么?

c++ - 使用 Visual C++ 6 项目的选项

c++ - Windows GDI 上下文 - LoadImage

c++ - boost::可选,带有 const 成员

Python装饰器添加类级变量

c++ - 如果通过单击任务栏按钮最小化程序,SW_SHOWNOACTIVATE 不起作用

C++/QML交互式组合框实现