mirror of
https://github.com/NixOS/nix
synced 2025-06-25 10:41:16 +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:
parent
b4bea57667
commit
09b2f1c477
1 changed files with 9 additions and 9 deletions
|
@ -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.
|
* Get a value for the specified key from an associate container.
|
||||||
*/
|
*/
|
||||||
template <class T>
|
template <class T, typename K>
|
||||||
const typename T::mapped_type * get(const T & map, const typename T::key_type & key)
|
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;
|
if (i == map.end()) return nullptr;
|
||||||
return &i->second;
|
return &i->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T, typename K>
|
||||||
typename T::mapped_type * get(T & map, const typename T::key_type & key)
|
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;
|
if (i == map.end()) return nullptr;
|
||||||
return &i->second;
|
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.
|
* 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::mapped_type & getOr(T & map,
|
||||||
const typename T::key_type & key,
|
K && key,
|
||||||
const typename T::mapped_type & defaultValue)
|
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;
|
if (i == map.end()) return defaultValue;
|
||||||
return i->second;
|
return i->second;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue