C2535,其模板类位于无序映射中(Microsoft Visual Studio 2015 CTP6)
C2535 with template-class in unordered_map (Microsoft Visual Studio 2015 CTP6)
我在尝试编译以下代码时遇到一个奇怪的C2535编译器错误:
template<int NUMBER>
class Container {
public:
bool operator==(const Container& other) const { return true; }
};
namespace std {
template <int NUMBER>
class hash<Container<NUMBER>> {
public:
size_t operator()(const Container<NUMBER> & state) const {
return 0;
}
};
}
int main(int argc, char* argv[]){
auto* b = new std::unordered_map< Container<1>, int>(); //C2535
}
注意,如果我使用自己的基于模板的Hasher
template<int NUMBER>
class Hash {
public:
size_t operator()(const Container<NUMBER> & state) const {
return 0;
}
};
int main(int argc, char* argv[]){
auto* b = new std::unordered_map< Container<1>, int, Hash<1>>();
}
代码编译得很好。我还记得,在Visual Studio 2013学习版中,代码的编译没有遇到任何问题。
问题:这是VS 2015的错误吗?还是这种行为在某种程度上符合标准?
实际上,这是由§14.5.1/4:中的一个微妙之处造成的
在重新声明中,部分专门化、显式专门化或类模板的显式实例化,类键应一致与原始类模板声明相同(7.1.6.3)。
并且,根据§20.9/2,hash
被声明为
标题
<functional>
摘要// 20.9.12, hash function primary template: template <class T> struct hash;
因此尝试
template <int NUMBER>
struct hash<Container<NUMBER>> { /*…*/ };
相反。
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 将函数类成员映射到类本身内部
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 使用std::函数映射对象方法
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- C++映射分割错误(核心转储)
- 内联映射初始化的动态atexit析构函数崩溃
- 使用"std::unordereded_map"映射到"std::list"对象
- 如何从多映射中删除特定的重复项
- 在未初始化映射的情况下,将值插入到映射的映射中
- QT通过C++添加映射QML项目
- 在c++中访问int到类对象的映射时出错
- 在C++中搜索嵌套多映射值
- 错误处理.将系统错误代码映射到泛型
- C++匿名结构作为std::映射值
- 如何从存储在std::映射中的std::集中删除元素
- 递归无序映射
- C2535,其模板类位于无序映射中(Microsoft Visual Studio 2015 CTP6)