Open In App

std::is_function template in C++ with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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/



Last Updated : 28 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads