我通过枚举专门化了一个模板
template<>
class specialized_class<user_option=CHOICE_ENUM::CHOSEN_OPTION>
{
public:
typedef second_class<user_option> obj_type;
在这里,为了定义second_class
,我需要知道用户选择了什么类型。我需要命名所选的选项。但是我的代码的第二行是非法语法。如何做我需要的?
虽然
typedef second_class<CHOICE_ENUM::CHOSEN_OPTION> obj_type;
解决问题。我宁愿避免这种方式,因为它使用的是魔术类型,并且类中的代码需要修改才能在其他地方使用。
最佳答案
如果您正在尝试减少特化中 CHOICE_ENUM::CHOSEN_OPTION
的出现。也许以下内容就足够了。
enum Enum
{
E1,
E2
};
template<Enum E>
class Foo
{
};
template<Enum E>
class Bar
{
};
template<>
class Foo<E2> // 1 occurrence to change if copy/pasted for a new specialization
{
public:
static const Enum enum_value = E2; // Last occurrence to change
typedef Bar<enum_value> obj_type;
};
关于c++ - 使用专门的模板并命名枚举选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29666703/