Skip to content
Related Articles

Related Articles

std::is_function template in C++ with Examples
  • Last Updated : 28 May, 2020
GeeksforGeeks - Summer Carnival Banner

The std::is_function of C++ STL is used to check whether the given type T is function or not. It returns the boolean value either true or false. Below is the syntax for the same:

Header File:

#include<type_traits>

Syntax:

template 
  <class T> 
  struct is_function;

Parameter: The template std::is_function accepts a single parameter T (Trait class) to check whether T is a function or not.

Return Values:



  • True: If T is a function type.
  • False: If T is not a function type.

Below programs illustrate the std::is_function template in C++ STL:

Program 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;
}
Output:
false
true
true
false
true

Program 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;
}
Output:
true
false
false
true
true

Reference: http://www.cplusplus.com/reference/type_traits/is_function/

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :