模板参数扣除与强类型枚举 [英] template argument deduction with strongly-typed enumerations

查看:182
本文介绍了模板参数扣除与强类型枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个正常(弱)枚举,我可以使用其枚举值作为非类型模板参数,如下:

If I have a normal (weak) enumeration, I can use its enumerated values as non-type template parameters, like so:

enum { Cat, Dog, Horse };

template <int Val, typename T> bool magic(T &t)
{
    return magical_traits<Val>::invoke(t);
}

并调用: magic< Cat> t)

我可以看到,如果我有一个强类型枚举并且不想硬编码枚举类型,我最终以:

as far as I can see, if I have a strongly-typed enumeration and don't want to hard-code the enumeration type, I end up with:

enum class Animal { Cat, Dog, Horse };

template <typename EnumClass, EnumClass EnumVal, typename T> bool magic(T &t)
{
    return magical_traits<EnumVal>::invoke(t);
}

现在我必须写: magic& ,Animal :: Cat>(t),这似乎是多余的。

and now I have to write: magic<Animal, Animal::Cat>(t), which seems redundant.

有任何方法可以避免输出枚举类和值,短于

Is there any way to avoid typing out both the enum class and the value, short of

#define MAGIC(E, T) (magic<decltype(E), E>(T));


推荐答案

很抱歉,

取出宏,放入一个可怕的命名标题,同事的清理脚本。希望最好的。

Take the macro, put it into a scary named header and protect it from your colleague's cleanup script. Hope for the best.

这篇关于模板参数扣除与强类型枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆