C ++中的is_fundamental模板

在本文中,我们将讨论C ++ STL中std::is_fundamental模板的工作,语法和示例。

is_ basic是<type_traits>头文件下的模板。该模板用于检查给定类型T是否为基本数据类型。

什么是基本类型?

基本类型是已在编译器本身中声明的内置类型。像int,float,char,double等。这些也称为内置数据类型。

用户定义的所有数据类型(例如:类,枚举,结构,引用或指针)都不属于基本类型。

语法

template <class T> is_fundamental;

参数

模板只能具有类型T的参数,并检查给定类型是否为最终类类型。

返回值

它返回一个布尔值,如果给定类型是基本数据类型,则返回true,如果给定类型不是基本数据类型,则返回false。

示例

Input: class final_abc;
   is_fundamental<final_abc>::value;
Output: False

Input: is_fundamental<int>::value;
Output: True

Input: is_fundamental<int*>::value;
Output: False

示例

#include <iostream>
#include <type_traits>
using namespace std;
class TP {
   //TP Body
};
int main() {
   cout << boolalpha;
   cout << "checking for is_fundamental:";
   cout << "\nTP: "<< is_fundamental<TP>::value;
   cout << "\nchar :"<< is_fundamental<char>::value;
   cout << "\nchar& :"<< is_fundamental<char&>::value;
   cout << "\nchar* :"<< is_fundamental<char*>::value;
   return 0;
}

输出结果

如果我们运行上面的代码,它将生成以下输出-

checking for is_fundamental:
TP: false
char : true
char& : false
char* : false

示例

#include <iostream>
#include <type_traits>
using namespace std;
int main() {
   cout << boolalpha;
   cout << "checking for is_fundamental:";
   cout << "\nint: "<< is_fundamental<int>::value;
   cout << "\ndouble :"<< is_fundamental<double>::value;
   cout << "\nint& :"<< is_fundamental<int&>::value;
   cout << "\nint* :"<< is_fundamental<int*>::value;
   cout << "\ndouble& :"<< is_fundamental<double&>::value;
   cout << "\ndouble* :"<< is_fundamental<double*>::value;
   return 0;
}

输出结果

如果我们运行上面的代码,它将生成以下输出-

checking for is_fundamental:
int: true
double : true
int& : false
int* : false
double& : false
double* : false