-
Notifications
You must be signed in to change notification settings - Fork 57
Comparators and projections
Most sorting algorithms in cpp-sort accept comparison and/or projection parameters. The library therefore considers these kinds of functions to be first-class citizens too and provides dedicated comparators, projections and tools to combine them and to solve common related problems.
All the functions and classes in cpp-sort that take comparison or projection functions as parameters expect Callable
parameters, which correspond to anything that can be used as the first parameter of std::invoke
. This allows to pass entities such as pointers to members or pointer to member functions to the sorting algorithms; it should work out-of-the-box without any wrapping needed on the user side.
Several of the miscellaneous utilities provided by the library are meant to interact with comparators and projections:
-
as_comparison
andas_projection
are used to make it explicit whether an ambiguous function object should be used for comparison or for projection. -
as_function
can be used to turn any Callable into an object invokable with regular parentheses. -
is_probably_branchless_comparison
andis_probably_branchless_projection
are type traits that can be used to mark whether functions are likely to be branchless when called with a specific type. -
identity
is the default projection returning the argument it is passed without modifying it.
Sorters and adapters in the library accept comparators taking their parameters by non-const
reference, and should work as expected as long as comparators do not actually modify their parameters in a way that affects the comparison result. This is mostly meant to support legacy comparators, but it also covers niche use cases such as when a comparator needs to "mark" the objects that have been compared in an intrusive fashion.
This additional guarantee is allowed by the resolution of LWG3031. However when a comparator can take its parameters by both const
and non-const
reference, it is required to return consistent results no matter which overload is used (see the LWG issue for an example of inconsistent results).
New in version 1.7.0
- Home
- Quickstart
- Sorting library
- Comparators and projections
- Miscellaneous utilities
- Tutorials
- Tooling
- Benchmarks
- Changelog
- Original research