Skip to content

Commit 98e0a72

Browse files
Add optional_lookup utility function
This provides a more ergonomic interface over map::find
1 parent c752d9d commit 98e0a72

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/util/optional_utils.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*******************************************************************\
2+
3+
Module: functions that are useful with optionalt
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.h"
13+
14+
#include <iterator>
15+
#include <map>
16+
17+
/// Lookup a key in a map, if found return the associated value, nullopt otherwise
18+
template <typename map_like_collectiont, typename keyt>
19+
auto optional_lookup(const map_like_collectiont &map, const keyt &key)
20+
-> optionalt<decltype(map.find(key)->second)>
21+
{
22+
auto const it = map.find(key);
23+
if(it != map.end())
24+
{
25+
return it->second;
26+
}
27+
return nullopt;
28+
}
29+
30+
#endif // CPROVER_UTIL_OPTIONAL_UTILS_H

0 commit comments

Comments
 (0)