Less Than Operators

The project I am working on at the moment contains a fair number of custom less than operators. They are usually required to use non-trivial types (e.g. pointers to structs) as keys in maps – fair enough so far. But what amazes me time and again are the subtle possibilities for failure when crafting one. Some people either seem to be oblivious to the fact that less than needs to induce a strict weak ordering (i.e. if a < b, then under no circumstances can b < a be true), or they don't think their implementation through. Neither do they write a test for it nor get it reviewed. To be fair, the environment favours speed of development somewhat over quality, but I don't think that this provides an entitlement to ignore debug assertions from the STL.
So if you ever find yourself in the situation where you need such an operator, remember – they aren't hard to write, but quite easy to get subtly wrong. And a debug assertion means something is seriously wrong.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s