📜  C++中的is_scalar模板

📅  最后修改于: 2021-05-30 02:14:02             🧑  作者: Mango

C++ STL的std :: is_scalar模板用于检查给定类型是否为标量类型。它返回一个显示相同值的布尔值。

语法

template < class T > struct is_scalar;

参数:此模板接受单个参数T(特质类),以检查T是否为标量类型。

返回值:该模板返回一个布尔值,如下所示:

  • True :如果类型是标量类型。
  • False :如果类型是非标量类型。

下面的程序说明了C++ STL中的std :: is_scalar模板:

程序1

// C++ program to illustrate
// std::is_scalar template
  
#include 
#include 
using namespace std;
  
class GFG1 {
};
  
enum class GFG2 {
    var1,
    var2,
    var3,
    var4
};
  
// main program
int main()
{
  
    cout << boolalpha;
    cout << "is_scalar:"
         << endl;
    cout << "GFG1: "
         << is_scalar::value
         << endl;
    cout << "GFG2: "
         << is_scalar::value
         << endl;
    cout << "int[10]: "
         << is_scalar::value
         << endl;
    cout << "int &: "
         << is_scalar::value
         << endl;
    cout << "char: "
         << is_scalar::value
         << endl;
  
    return 0;
}
输出:
is_scalar:
GFG1: false
GFG2: true
int[10]: false
int &: false
char: true

程序2

// C++ program to illustrate
// std::is_scalar template
  
#include 
#include 
using namespace std;
  
// main program
int main()
{
    class gfg {
    };
  
    cout << boolalpha;
    cout << "is_scalar:"
         << endl;
    cout << "int(gfg::*): "
         << is_scalar::value
         << endl;
    cout << "int *: "
         << is_scalar::value
         << endl;
    cout << "bool: "
         << is_scalar::value
         << endl;
    cout << "int(int): "
         << is_scalar::value
         << endl;
    return 0;
}
输出:
is_scalar:
int(gfg::*): true
int *: true
bool: true
int(int): false

程序3

// C++ program to illustrate
// std::is_scalar template
  
#include 
#include 
using namespace std;
  
// main program
union GFG {
    int var1;
    float var2;
};
  
int main()
{
  
    cout << boolalpha;
    cout << "is_scalar:"
         << endl;
    cout << "GFG:"
         << is_scalar::value
         << endl;
    cout << "float: "
         << is_scalar::value
         << endl;
    cout << "double: "
         << is_scalar::value
         << endl;
    cout << "char: "
         << is_scalar::value
         << endl;
    cout << "nullptr_t: "
         << is_scalar::value
         << endl;
  
    return 0;
}
输出:
is_scalar:
GFG:false
float: true
double: true
char: true
nullptr_t: true
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”