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

前端 未结 3 872
执笔经年
执笔经年 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:36

    It's actually interesting... I bumped into this question while using MSVC++. What I'm trying to do is:

    static std::unordered_map FunctionMap()
    {
        static std::unordered_map map;
        return map;
    }
    

    with Fun a function pointer type.

    During compilation, I get the following error:

    error C2338: The C++ Standard doesn't provide a hash for this type.
    ....
    _Kty=int (__thiscall Testje::* )(int,int)
    

    In a previous attempt I attempted to cast the function pointer to void*, which isn't allowed and doesn't compile (see: https://isocpp.org/wiki/faq/pointers-to-members#cant-cvt-memfnptr-to-voidptr for details). The reason is that a void* is a data pointer, while a function pointer is a code pointer.

    My conclusion so far is that it isn't allowed and it won't compile on MSVC++.

提交回复
热议问题