CBMC
optional_utils.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: functions that are useful with std::optional
4 
5 Author: Diffblue Ltd.
6 
7 \*******************************************************************/
8 
9 #ifndef CPROVER_UTIL_OPTIONAL_UTILS_H
10 #define CPROVER_UTIL_OPTIONAL_UTILS_H
11 
12 #include <optional>
13 
16 template <typename map_like_collectiont, typename keyt>
17 auto optional_lookup(const map_like_collectiont &map, const keyt &key)
18  -> std::optional<decltype(map.find(key)->second)>
19 {
20  auto const it = map.find(key);
21  if(it != map.end())
22  {
23  return it->second;
24  }
25  return {};
26 }
27 
28 #endif // CPROVER_UTIL_OPTIONAL_UTILS_H
auto optional_lookup(const map_like_collectiont &map, const keyt &key) -> std::optional< decltype(map.find(key) ->second)>
Lookup a key in a map, if found return the associated value, nullopt otherwise.