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:
#include <bits/stdc++.h>
#include <boost/type_traits/is_pointer.hpp>
#include <boost/typeof/typeof.hpp>
using namespace std;
int main()
{
cout << "is_pointer_type: \n" ;
cout << "int *: "
<< boost::is_pointer< int *>::value << "\n" ;
cout << "char *: "
<< boost::is_pointer< char *>::value
<< "\n" ;
int * a;
cout << "pointer to integer: "
<< boost::is_pointer< decltype (a)>::value
<< "\n" ;
int * b = new int [29];
cout << "pointer to 1-D array: "
<< boost::is_pointer< decltype (b)>::value
<< "\n" ;
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:
#include <bits/stdc++.h>
#include <boost/type_traits/is_pointer.hpp>
#include <boost/typeof/typeof.hpp>
using namespace std;
void fun( int x)
{
cout << "value of x is : " << x << "\n" ;
}
struct A {
int x;
char a[8];
};
class student {
int roll;
string name;
public :
student( int x, string y)
{
roll = x;
name = y;
}
string get_name()
{
return name;
}
int get_roll()
{
return roll;
}
};
int main()
{
cout << "is_pointer_type: \n" ;
void (*ptr)( int ) = &fun;
cout << "pointer to fun: "
<< boost::is_pointer< decltype (ptr)>::value
<< "\n" ;
A obj;
cout << "instance of a structure: "
<< boost::is_pointer< decltype (obj)>::value
<< "\n" ;
A* obj2;
cout << "pointer to instance of a structure: "
<< boost::is_pointer< decltype (obj2)>::value
<< "\n" ;
student s(11840220, "GeeksforGeeks" );
cout << "instance of a class: "
<< boost::is_pointer< decltype (s)>::value
<< "\n" ;
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
Share your thoughts in the comments
Please Login to comment...