【发布时间】:2016-08-04 16:06:08
【问题描述】:
假设我有这样的代码:
#include <cstdio>
enum Foo { Foo_A, Foo_B };
enum Bar { Bar_A, Bar_B };
template<typename enumeration, int enumerator>
struct tpl;
template<int enumerator>
struct tpl<Foo, enumerator>
{
tpl() { printf("Foo: %d\n", enumerator); }
};
template<int enumerator>
struct tpl<Bar, enumerator>
{
tpl() { printf("Bar: %d\n", enumerator); }
};
int main()
{
tpl<Foo, Foo_A> foo_a;
tpl<Foo, Foo_B> foo_b;
tpl<Bar, Bar_A> bar_a;
tpl<Bar, Bar_B> bar_b;
return 0;
};
有没有办法减少使用现场的“重复”? IE。我不能从枚举器“Foo_A”等中推断出枚举类型“Foo”并以某种方式在上面的模板代码中使用它吗?枚举类在这里有帮助吗?
【问题讨论】:
-
这段代码看起来像是解决某些问题的方法。有什么问题?