简体   繁体   English

具有枚举规范的模板模板类在MSVC ++编译器上失败:C3201

[英]Template template class with enum specification fails on MSVC++ Compiler: C3201

Code

Here is the SSCCE example of my problem: 以下是我的问题的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;
}

[ EDIT : Changing LibraryT<MyEnum::Enum, MyEnumTemplate> to LibraryT<typename MyEnum::Enum, MyEnumTemplate> has no effect] [ 编辑 :将LibraryT<MyEnum::Enum, MyEnumTemplate>更改为LibraryT<typename MyEnum::Enum, MyEnumTemplate>无效]

Error 错误

The functionality I desire is the ability to create a library based on an enum and a class which is specialized by that enum. 我想要的功能是能够创建一个基于枚举的库和一个由该枚举专门化的类。 Above is my first attempt. 以上是我的第一次尝试。 I believe it is 100% C++, and GCC backs me up and says it all works. 我相信它是100%C ++,而GCC支持我,并说它一切正常。 However, I want it to compile with the MSVC++ Compiler and it refuses: 但是,我希望它使用MSVC ++编译器进行编译,它拒绝:

error C3201: the template parameter list for class template 'MyEnumTemplate' 
  does not match the template parameter list for template parameter 'EnumStruct'

Question

Is there some way I can make the MSVC++ compiler [ EDIT : MSVC++ 11 Compiler (VS 2012)] like my code? 有没有什么方法可以像我的代码一样制作MSVC ++编译器[ 编辑 :MSVC ++ 11编译器(VS 2012)]? Either by some addition specifications or different approach? 通过一些添加规范或不同的方法?

Possible (but undesirable) Solution 可能(但不合需要)解决方案

Hard code the enum type to be some integral type (the underlying type). 硬编码枚举类型是一些整数类型(基础类型)。 Then no problems. 那没问题。 But then my library is operating on integrals instead of the enum type (undesirable, but working) 但是我的库正在使用积分而不是枚举类型(不受欢迎,但正在工作)

// 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;
}

This is a known bug in the Visual C++ compiler. 这是Visual C ++编译器中的已知错误。 See the following bug on Microsoft Connect for more information (the repro is slightly different, but the issue is effectively the same): 有关详细信息,请参阅Microsoft Connect上的以下错误(repro略有不同,但问题实际上是相同的):

C++ compiler bug - cannot use template parameters inside nested template declaration C ++编译器错误 - 无法在嵌套模板声明中使用模板参数

The recommended workaround is to use an integer type for the template parameter of the template template parameter, which is what you have done in your "possible (but undesirable) solution." 建议的解决方法是使用整数类型作为模板模板参数的模板参数,这是您在“可能(但不合需要)的解决方案中所做的”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM