Open In App

boost is_pointer template in C++

Improve
Improve
Like Article
Like
Save
Share
Report

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:




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


Output:

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

Program 2:




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


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


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