在C++中使用std::is_floating_point模板
C++ STL中的std::is_floating_point模板 用于检查给定类型是否为浮点值。它返回一个布尔值指示结果。
语法:
template < class T > struct is_floating_point;
参数: 该模板接受一个单一参数T(Trait类)来检查T是否为浮点类型。
返回值: 该模板返回以下布尔值:
- True: 如果类型为float。
- False: 如果类型不是浮点值。
以下程序说明了C++ STL中的std::is_floating_point模板:
程序1:
// C++程序以说明std::is_floating_point模板
#include <iostream>
#include <type_traits>
using namespace std;
//主程序
int main()
{
cout << std::boolalpha;
cout << "is_floating_point:" << endl;
cout << "char: "
<< is_floating_point<char>::value
<< endl;
cout << "int: "
<< is_floating_point<int>::value
<< endl;
cout << "float: "
<< is_floating_point<float>::value
<< endl;
return 0;
}
is_floating_point:
char: false
int: false
float: true
程序2:
// C++程序以说明is_floating_point模板
#include <iostream>
#include <type_traits>
using namespace std;
//主程序
int main()
{
cout << std::boolalpha;
cout << "is_floating_point:" << endl;
cout << "double: "
<< is_floating_point<double>::value
<< endl;
cout << "bool: "
<< is_floating_point<bool>::value
<< endl;
cout << "long int: "
<< is_floating_point<long int>::value
<< endl;
return 0;
}
is_floating_point:
double: true
bool: false
long int: false
程序3:
// C++程序以说明is_floating_point函数
#include <iostream>
#include <type_traits>
using namespace std;
//主程序
int main()
{
cout << boolalpha;
cout << "is_floating_point:" << endl;
cout << "wchar_t: "
<< is_floating_point<wchar_t>::value
<< endl;
cout << "long double: "
<< is_floating_point<long double>::value
<< endl;
cout << "unsigned short int: "
<< is_floating_point<unsigned short int>::value
<< endl;
return 0;
}
is_floating_point:
wchar_t: false
long double: true
unsigned short int: false