📜  C++中的std :: underlying_type与示例

📅  最后修改于: 2021-05-30 08:48:00             🧑  作者: Mango

C++ STL的std :: underlying_type模板位于头文件中。 C++ STL的std :: underlying_type模板用于获取枚举类型T的基础类型。

头文件:

#include

模板类别:

template 
struct underlying_type;

句法:

std::underlying_type::value

参数:模板std :: underlying_type接受单个参数T(Trait class)

返回值:模板std :: underlying_type返回枚举类型T的基础类型。

下面是演示C++中std :: underlying_type的程序:

程序:

// C++ program to illustrate
// std::underlying_type
#include 
#include 
using namespace std;
  
// ENUM Class GFG
enum GFG {};
  
// Class gfg
enum class gfg : int {};
  
// Driver Code
int main()
{
    bool GFG1
        = is_same::type>::value;
  
    bool gfg1
        = is_same::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;
}
输出:
underlying type for 'GFG' is unsigned
underlying type for 'gfg' is int

参考: http://www.cplusplus.com/reference/type_traits/underlying_type/

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”