Boost元分析如何返回自定义类型



我正试图用元语法创建一个简单的解析器,以演示如何返回自己的自定义类型。

如果我有一个自定义类型ValNum,我如何将其集成到一个基本的解析器中,以便它在匹配一个数字后由解析器返回?如果这是微不足道的,我深表歉意,但我一直在努力实现这一点。

template<int Value>
struct ValNum {
constexpr static int value = Value;
};

您可以使用transform函数和元函数。

这里有一个非常快速的返回自定义类型的示例(删除他们在链接中给出的identity元函数类,以及我在他们的官方GitHub中找到的几个示例(

// custom type here
template<int Value>
struct ValNum {
constexpr static int value = Value;
};
struct identity
{
template <class T>
struct apply
{
// ValNum is my custom type
using type = ValNum<T::type::value>; 
};
using type = identity;
};
typedef
grammar<_STR("int_token")>
::import<_STR("int_token"), token<transform<int_, identity>>>::type
expression;
typedef build_parser<entire_input<expression>> calculator_parser;
int main()
{
// hello is now our custom ValNum type
using hello = apply_wrap1<calculator_parser, _STR("13")>::type;
using std::cout;
using std::endl;
cout << hello::value << endl;
}

希望这能帮助到别人。