當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ std::is_compound模板用法及代碼示例


C++ STL的std::is_compound模板用於檢查類型是否為複合類型。它返回一個顯示相同的布爾值。

用法

template < class T > struct is_compound;

參數:此模板包含單個參數T(特質類),以檢查T是否為複合類型。

返回值:此模板返回一個布爾值,如下所示:

  • True:如果類型是複合類型。
  • False:如果類型是非複合類型。

以下示例程序旨在說明C++ STL中的is_compound模板:



程序1:

// C++ program to illustrate 
// is_compound template 
  
#include <iostream> 
#include <type_traits> 
using namespace std; 
  
// main program 
struct GFG1 { 
}; 
  
union GFG2 { 
    int var1; 
    float var2; 
}; 
  
int main() 
{ 
    cout << boolalpha; 
    cout << "is_compound:"
         << endl; 
    cout << "GFG1:"
         << is_compound<GFG1>::value 
         << endl; 
    cout << "GFG2:"
         << is_compound<GFG2>::value 
         << endl; 
    cout << "int:"
         << is_compound<int>::value 
         << endl; 
    cout << "int*:"
         << is_compound<int*>::value 
         << endl; 
    return 0; 
}
輸出:
is_compound:
GFG1:true
GFG2:true
int:false
int*:true

程序2:

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

程序3:

// C++ program to illustrate 
// is_compound template 
  
#include <iostream> 
#include <type_traits> 
using namespace std; 
  
// driver code 
int main() 
{ 
    class gfg { 
    }; 
  
    cout << boolalpha; 
    cout << "is_compound:"
         << endl; 
    cout << "int(gfg::*):"
         << is_compound<int(gfg::*)>::value 
         << endl; 
    cout << "float:"
         << is_compound<float>::value 
         << endl; 
    cout << "double:"
         << is_compound<double>::value 
         << endl; 
    cout << "int(int):"
         << is_compound<int(int)>::value 
         << endl; 
  
    return 0; 
}
輸出:
is_compound:
int(gfg::*):true
float:false
double:false
int(int):true




相關用法


注:本文由純淨天空篩選整理自rajasethupathi大神的英文原創作品 std::is_compound Template in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。