コード
これが私の問題のSSCCEの例です:
// My Library, which I want to take in the user's enum and a template class which they put per-enum specialized code
template <typename TEnum, template <TEnum> class EnumStruct>
struct LibraryT { /* Library stuff */ };
// User Defined Enum and Associated Template (which gets specialized later)
namespace MyEnum {
enum Enum {
Value1 /*, ... */
};
};
template <MyEnum::Enum>
struct MyEnumTemplate {};
template <>
struct MyEnumTemplate<MyEnum::Value1> { /* specialized code here */ };
// Then the user wants to use the library:
typedef LibraryT<MyEnum::Enum, MyEnumTemplate> MyLibrary;
int main() {
MyLibrary library;
}
[編集: に変更LibraryT<MyEnum::Enum, MyEnumTemplate>
しLibraryT<typename MyEnum::Enum, MyEnumTemplate>
ても効果はありません]
エラー
私が望む機能は、列挙型とその列挙型によって特化されたクラスに基づいてライブラリを作成する機能です。上は私の最初の試みです。私はそれが 100% C++ であると信じており、GCC は私をバックアップし、すべてが機能すると言っています。ただし、MSVC++ コンパイラでコンパイルしたいのですが、拒否されます。
error C3201: the template parameter list for class template 'MyEnumTemplate'
does not match the template parameter list for template parameter 'EnumStruct'
質問
MSVC++ コンパイラ [編集: MSVC++ 11 コンパイラ (VS 2012)] を私のコードのようにする方法はありますか? いくつかの追加仕様または別のアプローチのいずれかですか?
可能な (しかし望ましくない) 解決策
列挙型を何らかの整数型 (基になる型) にハード コードします。その後、問題はありません。しかし、私のライブラリは列挙型ではなく積分で動作しています(望ましくないが、機能しています)
// My Library, which I want to take in the user's enum and a template class which they put per-enum specialized code
typedef unsigned long IntegralType; // **ADDED**
template <template <IntegralType> class EnumStruct> // **CHANGED**
struct LibraryT { /* Library stuff */ };
// User Defined Enum and Associated Template (which gets specialized later)
namespace MyEnum {
enum Enum {
Value1 /*, ... */
};
};
template <IntegralType> // **CHANGED**
struct MyEnumTemplate {};
template <>
struct MyEnumTemplate<MyEnum::Value1> {};
// Then the user wants to use the library:
typedef LibraryT<MyEnumTemplate> MyLibrary; // **CHANGED**
int main() {
MyLibrary library;
}