Skip to content

Commit b1e1b4d

Browse files
authored
Merge pull request #12229 from ethereum/fixValueOrDefault
Fix util::valueOrDefault.
2 parents fd68cb1 + 73470ae commit b1e1b4d

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

libsolutil/CommonData.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,22 @@ template<
267267
typename MapType,
268268
typename KeyType,
269269
typename ValueType = std::decay_t<decltype(std::declval<MapType>().find(std::declval<KeyType>())->second)> const&,
270-
typename AllowCopyType = void*
270+
typename AllowCopyType = std::conditional_t<std::is_pod_v<ValueType> || std::is_pointer_v<ValueType>, detail::allow_copy, void*>
271271
>
272-
decltype(auto) valueOrDefault(MapType&& _map, KeyType const& _key, ValueType&& _defaultValue = {}, AllowCopyType = nullptr)
272+
decltype(auto) valueOrDefault(
273+
MapType&& _map,
274+
KeyType const& _key,
275+
ValueType&& _defaultValue = {},
276+
AllowCopyType = {}
277+
)
273278
{
274279
auto it = _map.find(_key);
275280
static_assert(
276281
std::is_same_v<AllowCopyType, detail::allow_copy> ||
277-
std::is_reference_v<decltype((it == _map.end()) ? _defaultValue : it->second)>,
282+
std::is_reference_v<decltype((it == _map.end()) ? std::forward<ValueType>(_defaultValue) : it->second)>,
278283
"valueOrDefault does not allow copies by default. Pass allow_copy as additional argument, if you want to allow copies."
279284
);
280-
return (it == _map.end()) ? _defaultValue : it->second;
285+
return (it == _map.end()) ? std::forward<ValueType>(_defaultValue) : it->second;
281286
}
282287

283288
namespace detail

0 commit comments

Comments
 (0)