Object as a key in CMap

I have a class with a name error_code. I use it as a key for std::mapand CMap(MFC). I can make it work for std::map, but not CMap. Can I find out how I can do this?

// OK!
std::map<error_code, int> m;
m[error_code(123)] = 888;

// error C2440: 'type cast' : cannot convert from 'error_code' to 'DWORD_PTR'
CMap <error_code, error_code&, int, int& > m; 
m[error_code(123)] = 888;

class error_code {
public:
    error_code() : hi(0), lo(0) {}
    error_code(unsigned __int64 lo) : hi(0), lo(lo) {}
    error_code(unsigned __int64 hi, unsigned __int64 lo) : hi(hi), lo(lo) {}

    error_code& operator|=(const error_code &e) {
        this->hi |= e.hi;
        this->lo |= e.lo;
        return *this;
    }

    error_code& operator&=(const error_code &e) {
        this->hi &= e.hi;
        this->lo &= e.lo;
        return *this;
    }

    bool operator==(const error_code& e) const {
        return hi == e.hi && lo == e.lo;
    }

    bool operator!=(const error_code& e) const {
        return hi != e.hi || lo != e.lo;
    }

    bool operator<(const error_code& e) const {
        if (hi == e.hi) {
            return lo < e.lo;
        }
        return hi < e.hi;
    }

    unsigned __int64 hi;
    unsigned __int64 lo;
};
+3
source share
1 answer

A quick trace indicates that support for the template function is causing an error:

template<class ARG_KEY>
AFX_INLINE UINT AFXAPI HashKey(ARG_KEY key)
{
    // default identity hash - works for most primitive values
    return (DWORD)(((DWORD_PTR)key)>>4);
}

A quick fix involves adding an implicit conversion function to the user type. I'm not sure what data will be stored, so just randomly select an attribute to form the required data.

class error_code {

    ...

    operator DWORD_PTR() const
    {
        return hi;
    }

    ...
}
+1
source

Source: https://habr.com/ru/post/1783361/


All Articles