代码
这是我的问题的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;
}