A technique for building flexible lexicographical comparators for C++ structs using pointers-to-members and C++14 template utilities. Instead of hand-writing separate comparator classes for each ordering, a generic `lex_compare` function accepts a comparator type and a variadic list of member pointers, using `std::index_sequence` and `std::tuple` to generate efficient memberwise comparisons at compile time. The resulting assembly shows zero overhead compared to hand-written code, with all template indirection eliminated by the optimizer.
Table of contents
Using std::tie() Copy link Link copied!Applying pointes to members Copy link Link copied!C++14 improvements Copy link Link copied!Performance Copy link Link copied!Wrap-up Copy link Link copied!Sort: