枚舉類型假設我有這樣的代碼:獲得來自枚舉
#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;
};
有沒有減少「重複」在使用場所的方法嗎?即我不能從枚舉數「Foo_A」中推導枚舉類型「Foo」,並在上面的模板代碼中使用它?一個枚舉類會在這裏幫助嗎?
這段代碼看起來像解決了一些問題。問題是什麼? –