C++ 中的 is_fundamental 模板
在本文中,我们将讨论 C++ STL 的 std::is_fundamental 模板的工作方式、语法和示例。
is_fundamental 是一个模板,位于 <type_traits> 头文件中。此模板用于检查给定的类型 T 是否为基础数据类型。
什么是基础类型?
基础类型是在编译器本身中已声明的内置类型。例如 int、float、char、double 等。这些类型也称为内置数据类型。
所有用户定义的数据类型(如类、枚举、结构、引用或指针)都不是基础类型的一部分。
语法
template <class T> is_fundamental;
参数
该模板只能有一个类型为 T 的参数,并检查给定的类型是否为最终类类型。
返回值
它返回一个布尔值,如果给定的类型是基础数据类型,则为真;如果给定的类型不是基础数据类型,则为假。
示例
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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP