Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GUID as std::map key

a dictionary was defined as the following :

typedef boost::tuple<conn_ptr, handler_ptr, rdp_ptr> conn_tuple;
typedef std::map<GUID, conn_tuple> conn_map; 

we got a compilation error:

Error 9 error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const GUID' (or there is no acceptable conversion) c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef

Then we solve it as:

struct GUIDComparer
{
    bool operator()(const GUID & Left, const GUID & Right) const
    {
        // comparison logic goes here
        if( (Left.Data1 == Right.Data1) && (Left.Data2 == Right.Data2) && 
            (Left.Data3 == Right.Data3) && (memcmp(Left.Data4 , Right.Data4,sizeof(Right.Data4))==0)  )
        {   
            return true;
        }
        return false;
    }
};
typedef boost::tuple<conn_ptr, handler_ptr, rdp_ptr> conn_tuple;
typedef std::map<GUID, conn_tuple, GUIDComparer> conn_map; 

Now, all compiled, but then we get an exception (invalid operator< )in run time.

I have no idea what is wrong, will be glad if someone could help

like image 655
feras Avatar asked Apr 24 '26 22:04

feras


1 Answers

Your GUIDComparer is comparing for equality. The functor you pass to a map must generate weak ordering - i.e. it must compare less or compare greater, not equal.

this will work:

struct GUIDComparer
{
    bool operator()(const GUID & Left, const GUID & Right) const
    {
        // comparison logic goes here
        return memcmp(&Left , &Right,sizeof(Right)) < 0;
    }
};
like image 137
Richard Hodges Avatar answered Apr 26 '26 10:04

Richard Hodges



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!