Related Articles

Related Articles

std::underlying_type in C++ with Example
  • Last Updated : 28 Jul, 2020

The std::underlying_type template of C++ STL is present in the <type_traits> header file. The std::underlying_type template of C++ STL is used to get the underlying type of enum type T.

Header File:

#include<type_traits>

Template Class:

template <class T>
struct underlying_type;

Syntax:

std::underlying_type<class T>::value

Parameter: The template std::underlying_type accepts a single parameter T(Trait class).



Return Value: The template std::underlying_type return the underlying type of enum type T.

Below is the program to demonstrate std::underlying_type in C++:

Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// std::underlying_type
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
  
// ENUM Class GFG
enum GFG {};
  
// Class gfg
enum class gfg : int {};
  
// Driver Code
int main()
{
    bool GFG1
        = is_same<unsigned,
                  typename underlying_type<GFG>::type>::value;
  
    bool gfg1
        = is_same<int,
                  typename underlying_type<gfg>::type>::value;
  
    cout << "underlying type for 'GFG' is "
         << (GFG1 ? "unsigned" : "non-unsigned")
         << endl;
  
    cout << "underlying type for 'gfg' is "
         << (gfg1 ? "int" : "non-int")
         << endl;
  
    return 0;
}

chevron_right


Output:

underlying type for 'GFG' is unsigned
underlying type for 'gfg' is int

Reference: http://www.cplusplus.com/reference/type_traits/underlying_type/

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.

My Personal Notes arrow_drop_up
Recommended Articles
Page :