boost is_pointer template in C++

The is_pointer template of Boost library is used to check whether the given type is a pointer or not. It returns a boolean value showing the same.

Header Files:

#include "boost/type_traits.hpp"
 or 
#include "boost/type_traits/is_pointer.hpp"

Template Class:



template <class T>
struct is_pointer : public true_type-or-false_type {};

If T is a pointer type then inherits from true_type else inherits from false_type.

Syntax:

boost::is_pointer::value
boost::is_pointer::value_type

Parameter: This template accepts a single parameter T(Trait class) to check whether T is a pointer type or not.

Accepted Arguments: This template accept the below arguments:

typename T
T *volatile
T *const volatile
T *const
T *

Return Value: This template returns a boolean value as shown below:

  • True: If the type is a pointer value.
  • False: If the type is a not pointer value.

Below programs illustrates the std::is_pointer template in C++ STL:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// std::is_floating_point template
#include <bits/stdc++.h>
#include <boost/type_traits/is_pointer.hpp>
#include <boost/typeof/typeof.hpp>
using namespace std;
  
// Main Program
int main()
{
    cout << "is_pointer_type: \n";
    cout << "int *: "
         << boost::is_pointer<int*>::value << "\n";
    cout << "char *: "
         << boost::is_pointer<char*>::value
         << "\n";
  
    // Pointer to integer
    int* a;
    cout << "pointer to integer: "
         << boost::is_pointer<decltype(a)>::value
         << "\n";
  
    // Pointer to 1D array
    int* b = new int[29];
    cout << "pointer to 1-D array: "
         << boost::is_pointer<decltype(b)>::value
         << "\n";
  
    // 1-D array
    int c[20];
    cout << "1-D array :"
         << boost::is_pointer<decltype(c)>::value
         << "\n";
  
    return 0;
}

chevron_right


Output:

is_pointer_type: 
int *: 1
char *: 1
pointer to integer: 1
pointer to 1-D array: 1
1-D array :0

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// std::is_floating_point template
#include <bits/stdc++.h>
#include <boost/type_traits/is_pointer.hpp>
#include <boost/typeof/typeof.hpp>
using namespace std;
  
// Function with an integer
// argument and void return type
void fun(int x)
{
    cout << "value of x is : " << x << "\n";
}
  
// A Structure
struct A {
    int x;
    char a[8];
};
  
// A Class
class student {
    int roll;
    string name;
  
public:
    // Student Class Constructor
    student(int x, string y)
    {
        roll = x;
        name = y;
    }
  
    // Member function to get the
    // name of a student
    string get_name()
    {
        return name;
    }
  
    // Member function to get the
    // roll number of a student
    int get_roll()
    {
        return roll;
    }
};
  
// Main Program
int main()
{
    cout << "is_pointer_type: \n";
  
    // Pointer to function
    void (*ptr)(int) = &fun;
    cout << "pointer to fun: "
         << boost::is_pointer<decltype(ptr)>::value
         << "\n";
  
    // Instance of A
    A obj;
    cout << "instance of a structure: "
         << boost::is_pointer<decltype(obj)>::value
         << "\n";
  
    // Pointer to instance of A
    A* obj2;
    cout << "pointer to instance of a structure: "
         << boost::is_pointer<decltype(obj2)>::value
         << "\n";
  
    // Instance of student
    student s(11840220, "GeeksforGeeks");
    cout << "instance of a class: "
         << boost::is_pointer<decltype(s)>::value
         << "\n";
  
    // Pointer to instance of student
    student* S;
    S = &s;
    cout << "pointer to instance of a class: "
         << boost::is_pointer<decltype(S)>::value
         << "\n";
  
    return 0;
}

chevron_right


Output:

is_pointer_type: 
pointer to fun: 1
instance of a structure: 0
pointer to instance of a structure: 1
instance of a class: 0
pointer to instance of a class: 1

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




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.