错误:“哈希”不是类模板

Fab*_*aff 1 c++ hash c++11

#include <unordered_map>
#include <memory>
#include <vector>

template<> // Voxel has voxel.position which is a IVec2 containing 2 values, it also has a bool value
struct hash<Voxel> {  
size_t operator()(const Voxel & k) const  
    {  
        return Math::hashFunc(k.position);  
    }  
};  

template<typename T> // This was already given
inline size_t hashFunc(const Vector<T, 2>& _key)
{
    std::hash<T> hashfunc;
    size_t h = 0xbd73a0fb;
    h += hashfunc(_key[0]) * 0xf445f0a9;
    h += hashfunc(_key[1]) * 0x5c23b2e1;
    return h;
}
Run Code Online (Sandbox Code Playgroud)

我的主要

int main()
{
    Voxel t{ 16,0,true };
    std::hash(t);
}
Run Code Online (Sandbox Code Playgroud)

现在我正在写std :: hash的专业化。现在,在线提交页面始终为我的代码返回以下错误。我不知道为什么和做错了什么。

error: 'hash' is not a class template struct hash<>
Run Code Online (Sandbox Code Playgroud)

error: no match for call to '(const std::hash<Math::Vector<int, 2ul> >)   (const Math::Vector<int, 2ul>&)' noexcept(declval<const_Hash((declval<const_Key&>()))>.
Run Code Online (Sandbox Code Playgroud)

我自己的编译器只会抛出

error: The argument list for "class template" std :: hash "" is missing.
Run Code Online (Sandbox Code Playgroud)

Max*_*kin 5

您专注std::hash<>于全局命名空间,但这是不正确的。

专业化必须在同一命名空间 中声明std。请参阅以下示例std::hash

// custom specialization of std::hash can be injected in namespace std
namespace std
{
    template<> struct hash<S>
    {
        typedef S argument_type;
        typedef std::size_t result_type;
        result_type operator()(argument_type const& s) const
        ...
Run Code Online (Sandbox Code Playgroud)


Gho*_*per 5

为了后代,我忘记了也收到相同的错误消息#include <functional>