c++ std::hash specilization trait compile error

c++ std::hash specilization trait compile error

本文关键字:compile error trait hash std c++ specilization      更新时间:2023-10-16

嗨,我正在尝试实现 std::hash 结构的 trait 类,但我无法从这里取得进展:

#include <iostream>
#include <functional>
namespace std {

template<>
    struct hash< Test::AckMsgType<Test::EACKMSGTYPE::ACKMSG_POST> >
    {
        typedef std::size_t value_type;
        typedef Test::AckMsgType<Test::EACKMSGTYPE::ACKMSG_POST> AckMsg;
        value_type operator()(const AckMsg & aAckMsg) const
        {
            value_type const h1 ( std::hash<int>()(aAckMsg.getProxyID()) );
            value_type const h2 ( std::hash<int>()(aAckMsg.getCmdID()) );
            value_type const h3 ( std::hash<int>()(aAckMsg.getHdrMsgId()) );
            return (h1 ^ (h2 << 1)) ^ (h3 << 1) ;
        }
    };
}
    namespace Test {

    enum class EACKMSGTYPE
    {
        ACKMSG_POST,
        ACKMSG_RELEASE
    };
    //definicion de los traits
    template<EACKMSGTYPE>
    class AckMsgType
    {};
    template<>
    class AckMsgType<EACKMSGTYPE::ACKMSG_POST>
    {
        public:
        explicit AckMsgType(const int & aID):ID(aID)
        {
        }
        void setCmdId(const int & aCmdId)
        {
          CmdID =aCmdId;
        }
        void setMsgHeaderId(const int & aHeaderId)
        {
            HdrMsgId=aHeaderId;
        }
        void buildAckIdentifier()
        {
        }
        int getProxyID()
        {
            return (ID);
        }
        int getCmdID() const
        {
            return CmdID;
        }
        int getHdrMsgId() const
        {
            return HdrMsgId;
        }
        private:
        const int ID;
        int CmdID;
        int HdrMsgId;
        int AckMsgId;
    };
    }
    int main()
    {
        return 0;
    }

看起来更像是你正在编写std::hash的专业化。为此,您可能需要在进行专业化之前定义类......