【问题标题】:Template template class with enum specification fails on MSVC++ Compiler: C3201具有枚举规范的模板模板类在 MSVC++ 编译器上失败:C3201
【发布时间】:2012-09-25 22:12:44
【问题描述】:

代码

这是我的问题的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&lt;MyEnum::Enum, MyEnumTemplate&gt; 更改为LibraryT&lt;typename MyEnum::Enum, MyEnumTemplate&gt; 无效]

错误

我想要的功能是能够创建一个基于枚举的库和一个由该枚举专门化的类。以上是我的第一次尝试。我相信它是 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++ 编译器 [EDIT: MSVC++ 11 Compiler (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?
  • @idjarn 最新:MSVC++ 11 编译器(包含在 VS 2012 中)
  • @ahenderson 我相信在这种情况下typename 部分是可选的,添加它并没有什么不同
  • 我没有深入查看代码,所以我不知道这是否相关,但可能值得注意的是,MSVC 尚未完全实现 C++11。因此,如果您的代码依赖于 C++11 功能,则可能值得检查 MSVC 是否实际实现了相关功能。即使有,这是较新的东西,因此编译器错误的可能性比“通常”要大。
  • @WeirdlyCheezy 我相信这是一个编译器错误,但我正在寻找一种方法来实现相同的结果,但要避免该错误。我很确定 MSVC++ 11 中的模板模板实现有点不稳定。

标签: c++ templates visual-c++ c++11 template-templates


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 2017-03-17
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多