From 09b2f1c4775ea9d1b9b3e55244efaf1bd3b0bea5 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Fri, 23 May 2025 15:29:55 -0400 Subject: [PATCH] More fexible typing for `get` in `util.hh` Co-authored-by: Sergei Zimmerman <145775305+xokdvium@users.noreply.github.com> --- src/libutil/include/nix/util/util.hh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/libutil/include/nix/util/util.hh b/src/libutil/include/nix/util/util.hh index 2361bf2e7..37d14d4fb 100644 --- a/src/libutil/include/nix/util/util.hh +++ b/src/libutil/include/nix/util/util.hh @@ -223,18 +223,18 @@ 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); + auto i = map.find(std::forward(key)); if (i == map.end()) return nullptr; 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); + auto i = map.find(std::forward(key)); if (i == map.end()) return nullptr; return &i->second; } @@ -242,12 +242,12 @@ typename T::mapped_type * get(T & map, const typename T::key_type & key) /** * Get a value for the specified key from an associate container, or a default value if the key isn't present. */ -template +template const typename T::mapped_type & getOr(T & map, - const typename T::key_type & key, + K && key, const typename T::mapped_type & defaultValue) { - auto i = map.find(key); + auto i = map.find(std::forward(key)); if (i == map.end()) return defaultValue; return i->second; }