具有枚举规范的模板模板类在 MSVC++ 编译器 C3201 上失败
Template template class with enum specification fails on MSVC++ Compiler: C3201
Code
这是我问题的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;
}
这是
Visual C++编译器中的一个已知错误。 有关详细信息,请参阅 Microsoft Connect 上的以下错误(重现略有不同,但问题实际上是相同的):
C++编译器错误 - 不能在嵌套模板声明中使用模板参数
建议的解决方法是对模板模板参数的模板参数使用整数类型,这是您在"可能(但不需要)的解决方案"中所做的。
相关文章:
- MSVC多行宏编译器错误
- __CHAR_BIT__宏是否为 MSVC 编译器预定义?
- 为什么 std::count 比 MSVC 编译器的普通循环慢,但与 GCC 相等?
- MSVC 中可能存在的编译器错误
- msvc 编译器(和其他编译器)如何知道要绑定到哪个 std 重载?
- 如何使用CLion和MSVC编译器工具链编译wxWidgets Hello World应用程序而没有错误?
- 为什么 MSVC C++编译器将一个简单的 Hello World 扩展为 4000 行汇编?
- 为什么使用 <uint8_t>MSVC 2015 编译器的 std::vector::insert 比 std::copy 快 5 倍?
- MSVC 编译器/链接器何时合成标量/矢量删除析构函数
- 使用 MSVC 套件时,在 Qt 中正确选择编译器
- 如何让MSVC编译器优化多步POD初始化?
- std::function, Clang 6.0 & MSVC (10.0017134.12) - 可能的 ABI 错误或必需的编译器标志
- 使用 MSVC C++编译器创建动态大小的数组
- C++ MSVC/GCC/Clang编译器错误
- GCC,Apple LLVM和MSVC编译器的不同部分的名称是什么?
- MSVC编译器拆卸代码来自if/else语句
- MSVC 编译器在调试模式下构建 Qt 5.10 时崩溃
- 将 QT Creator 5.8 与 MSVC 2015 编译器配合使用
- MSVC 2015-我程序中的SSE编译器错误或错误/未定义的行为
- 当将编译器从G 切换到MSVC时,为什么此开关语句不会返回任何内容