boost is_pointer template in C++

Let’s talk about boost is_pointer in C++.

The is_pointer template of the boost library. It tells if the given type is a pointer or not. It returns the value in boolean.

Header file used: #include <boost/type_traits>

Code Prototype:

#include <boost/type_traits> 
namespace std 
{ 
   template <typename T> struct is_pointer<shared_ptr<T>> : std::true_type {}; 
    template <typename T> struct is_pointer<shared_ptr<T const>> :  std::true_type {};
}

Here T is the trait class. If T is a pointer type then it inherits from true type otherwise false type.

Syntax:

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

To understand the workings of boost is_pointer, Let us study a code:

#include <iostream>
#include <type_traits>

int main() 
{  
    // Set the output to display boolean values as true/false
    std::cout << std::boolalpha;

    // Check whether int is a pointer
    std::cout << std::is_pointer<int>::value << std::endl;

    // Check whether int* is a pointer
    std::cout << std::is_pointer<int*>::value << std::endl;

    // Check whether int** is a pointer
    std::cout << std::is_pointer<int**>::value << std::endl;

    // Check whether char is a pointer
    std::cout << std::is_pointer<char>::value << std::endl;

    return 0;
}

 

Output:

false
true
true
false

Leave a Reply

Your email address will not be published. Required fields are marked *