Skip to content

Commit f3e3f75

Browse files
committed
More get / getOr improvements
- Use `const K`, not `K`, otherwise we don't get auto referencing of rvalues. - Generalized the deleted overloads, because we don't care what the key type is --- we want to get rid of anything that has an rvalue map type.
1 parent 2b0fd88 commit f3e3f75

File tree

1 file changed

+13
-10
lines changed
  • src/libutil/include/nix/util

1 file changed

+13
-10
lines changed

src/libutil/include/nix/util/util.hh

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ std::pair<std::string_view, std::string_view> getLine(std::string_view s);
197197
* Get a value for the specified key from an associate container.
198198
*/
199199
template<class T, typename K>
200-
const typename T::mapped_type * get(const T & map, K & key)
200+
const typename T::mapped_type * get(const T & map, const K & key)
201201
{
202202
auto i = map.find(key);
203203
if (i == map.end())
@@ -206,34 +206,37 @@ const typename T::mapped_type * get(const T & map, K & key)
206206
}
207207

208208
template<class T, typename K>
209-
typename T::mapped_type * get(T & map, K & key)
209+
typename T::mapped_type * get(T & map, const K & key)
210210
{
211211
auto i = map.find(key);
212212
if (i == map.end())
213213
return nullptr;
214214
return &i->second;
215215
}
216216

217-
/** Deleted because this is use-after-free liability. Just don't pass temporaries to this overload set. */
218-
template<class T>
219-
typename T::mapped_type * get(T && map, const typename T::key_type & key) = delete;
217+
/**
218+
* Deleted because this is use-after-free liability. Just don't pass temporaries to this overload set.
219+
*/
220+
template<class T, typename K>
221+
typename T::mapped_type * get(T && map, const K & key) = delete;
220222

221223
/**
222224
* Get a value for the specified key from an associate container, or a default value if the key isn't present.
223225
*/
224226
template<class T, typename K>
225-
const typename T::mapped_type & getOr(T & map, K & key, const typename T::mapped_type & defaultValue)
227+
const typename T::mapped_type & getOr(T & map, const K & key, const typename T::mapped_type & defaultValue)
226228
{
227229
auto i = map.find(key);
228230
if (i == map.end())
229231
return defaultValue;
230232
return i->second;
231233
}
232234

233-
/** Deleted because this is use-after-free liability. Just don't pass temporaries to this overload set. */
234-
template<class T>
235-
const typename T::mapped_type &
236-
getOr(T && map, const typename T::key_type & key, const typename T::mapped_type & defaultValue) = delete;
235+
/**
236+
* Deleted because this is use-after-free liability. Just don't pass temporaries to this overload set.
237+
*/
238+
template<class T, typename K>
239+
const typename T::mapped_type & getOr(T && map, const K & key, const typename T::mapped_type & defaultValue) = delete;
237240

238241
/**
239242
* Remove and return the first item from a container.

0 commit comments

Comments
 (0)