C++ 如何为模板类实现std::hash

C++ 如何为模板类实现std::hash,c++,c++11,templates,C++,C++11,Templates,我有一个模板类,如下所示: template <int N, class TypeId> class Indexer { ... } namespace std { template <int N, class TypeId> struct hash<Indexer<N, TypeId> > { size_t operator()(const Indexer<N, TypeId>& id) const noexcept

我有一个模板类,如下所示:

template <int N, class TypeId> class Indexer {
...
}
namespace std {
template <int N, class TypeId>
struct hash<Indexer<N, TypeId> > {
    size_t operator()(const Indexer<N, TypeId>& id) const noexcept {
        ...
    }
};
}
它也非常类似于。
不幸的是,这不起作用,只会产生一系列毫无帮助的错误。有什么见解吗?

看起来您在索引器类的定义末尾缺少了一个分号

这项工作:

#include <functional>

template <int N, class TypeId> struct Indexer {};

namespace std {
template <int N, class TypeId>
struct hash<Indexer<N, TypeId> > {
   size_t operator()(const Indexer<N, TypeId>& id) const noexcept { return 0; }
};
}

int main() {
   return 0;
}
#包括
模板结构索引器{};
名称空间标准{
模板
结构散列{
size_t运算符()(const Indexer&id)const noexcept{return 0;}
};
}
int main(){
返回0;
}

“这不起作用”不是一个有用的问题描述。如果错误对您有帮助,您无需询问。但事实上,他们对你没有帮助并不意味着他们对我们没有帮助,你希望我们解决这个问题。发布完整的错误消息,以及a(复制它们的代码)。错误消息的第一行不是以“in file included from”或“in instantiation of”或“required from here”开头的是什么?我认为
std::hash
的部分专门化没有错,您需要显示实际错误(以及相关代码)请注意,“模板类”相当容易引起误解。你还没有上课
Indexer
是一种“类模板”,一种用于创建类的模板。模板实例化
索引器
将是一个类。分号在那里。该类已在其他地方使用!