A comparator object for a variant
type which is appropriate to use with std::set or
std::map.
|
expression |
value |
|---|---|
|
|
any |
|
|
A less-than comparator object for |
|
|
True if |
In header <strict_variant/variant_compare.hpp>.
namespace strict_variant { template <typename T, template <typename> class ComparatorTemplate = std::less, typename WhichComparator_t = std::less<int>> struct variant_comparator; }
variant
type.
std::less, which is the default. ComparatorTemplate<V>
should produce a valid less-than comparator for each value type V of the variant.
int type, such as std::less<int>, which is the default. This is used
to compare the which
values.
![]() |
Caution |
|---|---|
|
A less-than comparator is said to induce an equality-comparator by the following identity: a == b if and only if !(a < b) && !(b < a)
If a non-default |
Note that even when <strict_variant/variant_compare.hpp>
is included, variant does
still not have a operator <
overload for comparisons. Basically we consider that this is too error-prone.
See a related
discussion.
You can use strict_variant
with std::set like so:
using var_t = variant<std::string, int>; using set_t = std::set<var_t, variant_comparator<var_t>>;
An alternate method is to specialize std::less
for variant types:
namespace std { template <typename... Types> struct less<variant<Types...>> : variant_comparator<variant<Types...>> {}; } // end namespace std using var_t = variant<std::string, int>; using set_t = std::set<var_t>;
This may save some typing if you often use variant
in associative containers, but it is also less explicit.