C++ Logo

std-proposals

Advanced search

std::underlying_value

From: Kosher Yosher <goekhan.oezdogan_at_[hidden]>
Date: Sun, 20 Oct 2019 11:49:08 +0100
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

Received on 2019-10-20 05:51:35