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:
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
enum GFG {};
enum class gfg : int {};
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;
}
|
Output:
underlying type for 'GFG' is unsigned
underlying type for 'gfg' is int
Reference: http://www.cplusplus.com/reference/type_traits/underlying_type/
Share your thoughts in the comments
Please Login to comment...