代碼
這裏是我的問題SSCCE例如:模板模板類的MSVC++編譯器失敗:C3201
// 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;
}
如果它最終是相關的,VC++ 2010或2012? – ildjarn
@idjarn最近的一個:MSVC++ 11編譯器(它包含在VS 2012中) –
@ahenderson我相信在這種情況下'typename'部分是可選的,添加它並沒有區別 –