std::is_member_function_pointer template in C++ with Examples
The std::is_member_function_pointer template of C++ STL is present in the <type_traits> header file. The std::is_member_function_pointer template of C++ STL is used to check whether the T is a pointer to non-static member function or not. It return the boolean value true if T is pointer to non-static member function, otherwise return false.
Header File:
#include<type_traits>
Template Class:
template <class T> struct is_member_function_pointer;
Syntax:
std::is_member_function_pointer::value
Parameter: The template std::is_member_function_pointer accepts a single parameter T(Trait class) to check whether T is pointer to non-static member function or not.
Return Value: This template std::is_member_object_pointer returns a boolean variable as shown below:
- True: If the type T is a pointer to non-static member function type.
- False: If the type T is not a pointer to non-static member function type.
Below is the program to demonstrate std::is_member_function_pointer in C++:
Program 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
Program 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
Reference: http://www.cplusplus.com/reference/type_traits/is_member_function_pointer/
Please Login to comment...