A C++ article of mine has been published on the Pluralsight blog. It shows how to pragmatically use C++11’s std::tie to easily implement lexicographical comparisons of custom data types (the concept of lexicographical comparison is introduced in that post as well).

Basically, instead of writing a long (and potentially bug-prone) sequence of if statements, std::tie can be invoked to build tuples, which in turn can be compared using std::tuple’s already-defined operator< overload.

I also showed a common error that can happen when calling std::tie, and how to fix it using std::make_tuple.

An important take-away is that if there are tools already tested and available in the C++ standard library, it’s better to use them than attempting to reinvent the wheel writing boilerplate bug-prone “low-level” C++ code.