Namespaces
Variants
Views
Actions

std::underlying_type

From cppreference.com
< cpp‎ | types
Revision as of 01:24, 12 June 2012 by P12bot (Talk | contribs)

Template:cpp/types/sidebar Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <type_traits>
</td>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td >
template< class T >
struct underlying_type;
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> Template:ddcl list end

Defines a member typedef type of type that is the underlying type for the enumeration T.

Member types

Template:tdcl list hitemTemplate:tdcl list itemTemplate:tdcl list end

Notes

Each enumeration type has an underlying type, which can be

1. Specified explicitly (both scoped and unscoped enumerations)

2. Omitted, in which case it is int for scoped enumerations or an implementation-defined integral type capable of representing all values of the enum (for unscoped enumerations)

Example

#include <iostream>
#include <type_traits>
 
enum e1 {};
enum class e2: int {};
 
int main() {
    bool e1_type = std::is_same<
        unsigned
       ,typename std::underlying_type<e1>::type
    >::value; 
 
    bool e2_type = std::is_same<
        int
       ,typename std::underlying_type<e2>::type
    >::value;
 
    std::cout
    << "underlying type for 'e1' is " << (e1_type?"unsigned":"non-unsigned") << '\n'
    << "underlying type for 'e2' is " << (e2_type?"int":"non-int") << '\n';
}

Output:

underlying type for 'e1' is unsigned
underlying type for 'e2' is int