Can std::hash be used to hash function pointers?

前端 未结 3 832
执笔经年
执笔经年 2021-01-03 22:56

Can the C++11 std::hash type be used to hash function pointers? There is a hash partial specialization defined as

template 

        
3条回答
  •  抹茶落季
    2021-01-03 23:53

    Great question. I don't know the answer for sure, and I'm happy to defer to anyone with better knowledge than me, but my thinking is that even though function pointers aren't the same as data pointers, they are pointers nonetheless: so the std::hash partial specialisation should be applied.

    For what it's worth, the following compiles without warnings even with -pendantic in g++ 4.8.1 and clang 3.3, and works as expected:

    #include 
    #include 
    
    void func1(int) {}
    void func2(int) {}
    
    int main()
    {
        typedef void (*func_type) (int);
    
        std::hash hash;
    
        std::cout << hash(func1) << std::endl;
        std::cout << hash(func2) << std::endl;
    
    }
    

    I'd be really interested if anyone has any references to the standard to back this up though.

提交回复
热议问题