C++ enum class std::size_t implicit conversion
There is no implicit conversion here. From enum:
There are no implicit conversions from the values of a scoped enumerator to integral types, although static_cast may be used to obtain the numeric value of the enumerator.
So, you have to use static_cast
.
There are some workarounds which are based on static_cast
. For instance, one might make use of std::underlying_type
:
template<typename T>
constexpr auto get_idx(T value)
{
return static_cast<std::underlying_type_t<T>>(value);
}
And then:
const auto& key = std::get<get_idx(ParameterKey::KEY)>(*parameterPointer);
The whole purpose of enum class
is to not be implicitly convertible to int
, so there is no implicit conversion.
You could create your own get
version:
template <ParameterKey key, typename Tuple>
decltype(auto) get(Tuple &&tuple) {
return std::get<static_cast<std::underlying_type_t<ParameterKey>>(key)>(tuple);
}
Then:
const auto& key = get<ParameterKey::KEY>(*parameterPointer);