1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-24 22:11:15 +02:00

More fexible typing for get in util.hh

Co-authored-by: Sergei Zimmerman <145775305+xokdvium@users.noreply.github.com>
This commit is contained in:
John Ericson 2025-05-23 15:29:55 -04:00 committed by John Ericson
parent b4bea57667
commit 09b2f1c477

View file

@ -223,18 +223,18 @@ std::pair<std::string_view, std::string_view> getLine(std::string_view s);
/**
* Get a value for the specified key from an associate container.
*/
template <class T>
const typename T::mapped_type * get(const T & map, const typename T::key_type & key)
template <class T, typename K>
const typename T::mapped_type * get(const T & map, K && key)
{
auto i = map.find(key);
auto i = map.find(std::forward<K>(key));
if (i == map.end()) return nullptr;
return &i->second;
}
template <class T>
typename T::mapped_type * get(T & map, const typename T::key_type & key)
template <class T, typename K>
typename T::mapped_type * get(T & map, K && key)
{
auto i = map.find(key);
auto i = map.find(std::forward<K>(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 <class T>
template <class T, typename K>
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<K>(key));
if (i == map.end()) return defaultValue;
return i->second;
}