Fix util::valueOrDefault.

This commit is contained in:
Daniel Kirchner 2021-11-03 16:10:14 +01:00
parent ef8911a629
commit 73470aed6a

View File

@ -267,17 +267,22 @@ template<
typename MapType, typename MapType,
typename KeyType, typename KeyType,
typename ValueType = std::decay_t<decltype(std::declval<MapType>().find(std::declval<KeyType>())->second)> const&, typename ValueType = std::decay_t<decltype(std::declval<MapType>().find(std::declval<KeyType>())->second)> const&,
typename AllowCopyType = void* typename AllowCopyType = std::conditional_t<std::is_pod_v<ValueType> || std::is_pointer_v<ValueType>, detail::allow_copy, void*>
> >
decltype(auto) valueOrDefault(MapType&& _map, KeyType const& _key, ValueType&& _defaultValue = {}, AllowCopyType = nullptr) decltype(auto) valueOrDefault(
MapType&& _map,
KeyType const& _key,
ValueType&& _defaultValue = {},
AllowCopyType = {}
)
{ {
auto it = _map.find(_key); auto it = _map.find(_key);
static_assert( static_assert(
std::is_same_v<AllowCopyType, detail::allow_copy> || std::is_same_v<AllowCopyType, detail::allow_copy> ||
std::is_reference_v<decltype((it == _map.end()) ? _defaultValue : it->second)>, std::is_reference_v<decltype((it == _map.end()) ? std::forward<ValueType>(_defaultValue) : it->second)>,
"valueOrDefault does not allow copies by default. Pass allow_copy as additional argument, if you want to allow copies." "valueOrDefault does not allow copies by default. Pass allow_copy as additional argument, if you want to allow copies."
); );
return (it == _map.end()) ? _defaultValue : it->second; return (it == _map.end()) ? std::forward<ValueType>(_defaultValue) : it->second;
} }
namespace detail namespace detail