Consider the struct:
Using it in a std::set will generate a compile time error.
binary ‘<‘: no operator found which takes a left-hand operand of type ‘const ProductKey’
std::set internal structure calls for a less functor that given, two ProductKey objects, will determine which should be placed in front of the other in an ordered list.
by default std::set uses std::less, as we can see from std::set full declaration (line 4):
The default less operator employs the ‘<’ operator to compare the two keys:
As our ProductKey do not have a ‘<’ operator defined for it, compilation fails.
One possible solution is to overload the ‘< ‘ operator for the ProductKey user type. Once it is defined, the default std::less implementation can use this operator during compilation. However, this will actually be a case of doing too much. The only consumer of this new ‘<’ operator will be the associative container. There is no need to expose the new operator to any other component which includes ProductKey (if nobody needs it don’t implement it!). It will be more prudent to do the minimum needed and simply overload the less functor instead.
defining it in the std namespace will enable us to plug our new less functor into the default std::set declaration:
the empty <> after the template keyword in line 1 indicate that this is a specialized template. That is , when the compiler tries to instantiate the less<T> functor where T is ProductKey it will consider our template definition instead of the more general std::less functor defined in the standard library.
We can improve the less functor’s implementation by using std::tie. This function, included in <tuple>, creates a tuple out of the values supplied to it , Since ‘< ‘ is defined for the tuple type by the standard library , our std::less functor can be rewritten as
which is no only more readable, It’s also less error prone and not as boring to write!
So the complete example is:
Plugging your code into STL’s containers and algorithms, Enables you to make your code more robust and maintainable with less effort. Most important it can make it more fun to write.