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


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

C++ STL的std::is_function用於檢查給定的類型T是否為function。它返回布爾值true或false。以下是相同的語法:

頭文件:

#include<type_traits>

用法:

template 
  <class T> 
  struct is_function;

參數:模板std::is_function接受單個參數T(Trait類)以檢查T是否為函數。

返回值:



  • 是:如果T是函數類型。
  • False:如果T不是函數類型。

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

程序1:

// C++ program to illustrate 
// is_function template 
  
#include <bits/stdc++.h> 
#include <type_traits> 
using namespace std; 
  
struct GeeksforGeeks { 
    int func() const&; 
}; 
  
template <typename> 
struct Computer { 
}; 
  
template <class A, class B> 
struct Computer<B A::*> { 
    using member_type = B; 
}; 
  
int GFG(); 
  
int main() 
{ 
    cout << boolalpha; 
    cout << is_function<GeeksforGeeks>::value 
         << endl; 
    cout << is_function<int(int)>::value 
         << endl; 
    cout << is_function<decltype(GFG)>::value 
         << endl; 
    cout << is_function<int>::value 
         << endl; 
  
    using A = Computer<decltype( 
        &GeeksforGeeks::func)>::member_type; 
    cout << is_function<A>::value 
         << endl; 
  
    return 0; 
}
輸出:
false
true
true
false
true

程序2:

// C++ program to illustrate 
// is_function template 
  
#include <bits/stdc++.h> 
#include <type_traits> 
using namespace std; 
  
struct GeeksforGeeks { 
    int func() const&; 
}; 
  
template <typename> 
struct Computer { 
}; 
  
template <class A, class B> 
struct Computer<B A::*> { 
    using member_type = B; 
}; 
  
int GFG(); 
  
int main() 
{ 
    cout << boolalpha; 
    cout << is_function<int(int)>::value 
         << endl; 
    cout << is_function<GeeksforGeeks>::value 
         << endl; 
    cout << is_function<int>::value 
         << endl; 
    cout << is_function<decltype(GFG)>::value 
         << endl; 
  
    using A = Computer<decltype( 
        &GeeksforGeeks::func)>::member_type; 
    cout << is_function<A>::value 
         << endl; 
  
    return 0; 
}
輸出:
true
false
false
true
true

參考: http://www.cplusplus.com/reference/type_traits/is_function/




相關用法


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