std::is_function template in C++ with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

false
true
true
false
true

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

true
false
false
true
true

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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.