2012-10-04 53 views
10

代码

这里是我的问题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; 
} 
+0

如果它最终是相关的,VC++ 2010或2012? – ildjarn

+0

@idjarn最近的一个:MSVC++ 11编译器(它包含在VS 2012中) –

+0

@ahenderson我相信在这种情况下'typename'部分是可选的,添加它并没有区别 –

回答

3

这是在Visual C++编译器的一个已知的bug。请参阅Microsoft Connect上的以下错误的详细信息(摄制略有不同,但问题是实际上是相同的):

C++ compiler bug - cannot use template parameters inside nested template declaration

建议的解决方法是使用一个整数类型的模板模板参数的模板参数,这是您在“可能的(但不受欢迎的)解决方案中所做的”。