C++ Logo

STD-PROPOSALS

Advanced search

Subject: [std-proposals] std::underlying_value
From: Kosher Yosher (goekhan.oezdogan_at_[hidden])
Date: 2019-10-20 05:49:08


Hi,

With C++11 we have std::underlying_type to get the integer type of an enum
class. It feels like we should add the complementing std::underlying_value
function to get the integer value for an enum class value.

I would suggest the following constexpr to get this value:

    template<typename T>
    constexpr auto underlying_value(T _enum_value)
    {
        return static_cast<typename
::std::underlying_type<T>::type>(_enum_value);
    }

(auto return type for brevity here)

Use cases:
1) Legacy codebases
2) Networking code / Serialization
3) Enum-value as array-indexing

Thanks!
Gokhan



STD-PROPOSALS list run by std-proposals-owner@lists.isocpp.org

Standard Proposals Archives on Google Groups