'Can you tell me what the less<pointer> does in this map typedef

I'm looking at some code written by someone else trying to bug fix and have this type definition

typedef map<RPacket *, CLocalLink *, less<RPacket *> > CPacketToLocalLinkMap;

I'm not sure what the less<RPacket *> is doing



Solution 1:[1]

std::map is a class template whose third template parameter named Compare is a comparison function which is used to sort the keys. This third parameter Compare has a default argument std::less<Key>.

I'm not sure what the less<RPacket *> is doing

So, by explicitly writing:

less<RPacket *>

you're explicitly saying that this less<RPacket *> should be used as the comparison function to sort the keys instead of the default argument(which in your case is the same as less<RPacket *>).

std::less is itself a class template used for performing comparison which unless specialized, invokes operator< on type T.

template< class T = void > struct less;    since C++14

As already noted above, in your case there is no need to explicitly pass less<RPacket*> as the third argument since it is the same as the default argument. This means that you can just write:

//---------------------------------v------------------------>no need for the third template argument
typedef map<RPacket *, CLocalLink * > CPacketToLocalLinkMap; //this is equivalent to what you wrote 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1