From eebb25a39c42f8b936b4d59f74f240898be44acd Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 24 Feb 2020 04:31:52 -0500 Subject: [PATCH] hecl/hecl: Allow CaseInsensitiveCompare to be used with heterogenous lookup It's quite common to see maps or sets that make use of a string->object association, usually as . However, this can result in inefficient code when performing lookups or indexing, as something like: std::map some_map; ... auto iter = some_map.find("Name"); ... will result in a std::string instance being constructed around "Name", which is less than ideal. With a heterogenous comparator, however (such as std::less<>), like: std::map> we allow the container to do automatic type deduction and comparisons. This allows types comparable to the key type to be used in find calls, etc, without actually needing to construct the key type around the other type. The main way to enable containers to perform such behavior is by defining a type named is_transparent within the comparator type. --- hecl/include/hecl/hecl.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hecl/include/hecl/hecl.hpp b/hecl/include/hecl/hecl.hpp index 47c311dbe..37e43ca2e 100644 --- a/hecl/include/hecl/hecl.hpp +++ b/hecl/include/hecl/hecl.hpp @@ -505,6 +505,9 @@ public: * @brief Case-insensitive comparator for std::map sorting */ struct CaseInsensitiveCompare { + // Allow heterogenous lookup with maps that use this comparator. + using is_transparent = void; + bool operator()(std::string_view lhs, std::string_view rhs) const { return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), [](char lhs, char rhs) { return std::tolower(static_cast(lhs)) < std::tolower(static_cast(rhs));