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


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

<type_traits>頭文件中提供了C++ STL的std::is_member_function_pointer模板。 C++ STL的std::is_member_function_pointer模板用於檢查T是否是指向非靜態成員函數的指針。如果T是指向非靜態成員函數的指針,則返回布爾值true,否則返回false。

頭文件:

#include<type_traits>

模板類別:

template <class T>
struct is_member_function_pointer;

用法:

std::is_member_function_pointer::value 

參數:模板std::is_member_function_pointer接受單個參數T(Trait類),以檢查T是否是指向非靜態成員函數的指針。



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

  • True:如果類型T是指向非靜態成員函數類型的指針。
  • False:如果類型T不是指向非靜態成員函數類型的指針。

下麵是演示C++中std::is_member_function_pointer的程序:

程序1:

// C++ program to illustrate 
// std::is_member_function_pointer 
#include <bits/stdc++.h> 
#include <type_traits> 
using namespace std; 
  
// Declare a structure 
class GFG { 
  
public:
    int gfg; 
}; 
  
class A { 
}; 
  
// Driver Code 
int main() 
{ 
  
    // Object to class GFG 
    int GFG::*pt = &GFG::gfg; 
    cout << boolalpha; 
  
    // Check if GFG* is a member function 
    // pointer or not 
    cout << "GFG*:"
         << is_member_function_pointer<GFG*>::value 
         << endl; 
  
    // Check if int GFG::* is a member 
    // function pointer or not 
    cout << "int GFG::* "
         << is_member_function_pointer<int GFG::*>::value 
         << endl; 
  
    // Check if int A::* is a member 
    // function pointer or not 
    cout << "int A::* "
         << is_member_function_pointer<int A::*>::value 
         << endl; 
  
    // Check if int A::*() is a member 
    // function pointer or not 
    cout << "int A::*() "
         << is_member_function_pointer<int (A::*)()>::value 
         << endl; 
  
    return 0; 
}
輸出:
GFG*:false
int GFG::* false
int A::* false
int A::*() true

程序2:

// C++ program to illustrate 
// std::is_member_function_pointer 
#include <bits/stdc++.h> 
#include <type_traits> 
using namespace std; 
  
// Declare a structure 
struct A { 
    void fn(){}; 
}; 
  
struct B { 
    int x; 
}; 
  
// Driver Code 
int main() 
{ 
    void (A::*pt)() = &A::fn; 
    cout << boolalpha; 
  
    cout << "A*:"
         << is_member_function_pointer<A*>::value 
         << endl; 
  
    cout << "void(A::*)():"
         << is_member_function_pointer<void (A::*)()>::value 
         << endl; 
  
    cout << "B*:"
         << is_member_function_pointer<B*>::value 
         << endl; 
  
    cout << "void(B::*)():"
         << is_member_function_pointer<void (B::*)()>::value 
         << endl; 
  
    return 0; 
}
輸出:
A*:false
void(A::*)():true
B*:false
void(B::*)():true

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




相關用法


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