From 0ef818dd927f538769ee68ac3d91347f273842ef Mon Sep 17 00:00:00 2001 From: John Ericson Date: Fri, 23 May 2025 15:29:55 -0400 Subject: [PATCH] More flexible typing for `get` in `util.hh` This is good for e.g. `std::string_view` and `StringMap`. Needed by #11139 Co-authored-by: Sergei Zimmerman <145775305+xokdvium@users.noreply.github.com> --- src/libutil/include/nix/util/util.hh | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/libutil/include/nix/util/util.hh b/src/libutil/include/nix/util/util.hh index 765da8698..dd6294c2a 100644 --- a/src/libutil/include/nix/util/util.hh +++ b/src/libutil/include/nix/util/util.hh @@ -196,8 +196,8 @@ std::pair getLine(std::string_view s); /** * Get a value for the specified key from an associate container. */ -template -const typename T::mapped_type * get(const T & map, const typename T::key_type & key) +template +const typename T::mapped_type * get(const T & map, K & key) { auto i = map.find(key); if (i == map.end()) @@ -205,8 +205,8 @@ const typename T::mapped_type * get(const T & map, const typename T::key_type & return &i->second; } -template -typename T::mapped_type * get(T & map, const typename T::key_type & key) +template +typename T::mapped_type * get(T & map, K & key) { auto i = map.find(key); if (i == map.end()) @@ -221,9 +221,8 @@ typename T::mapped_type * get(T && map, const typename T::key_type & key) = dele /** * Get a value for the specified key from an associate container, or a default value if the key isn't present. */ -template -const typename T::mapped_type & -getOr(T & map, const typename T::key_type & key, const typename T::mapped_type & defaultValue) +template +const typename T::mapped_type & getOr(T & map, K & key, const typename T::mapped_type & defaultValue) { auto i = map.find(key); if (i == map.end())