is_pointer Template in C++

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of std::is_pointer template in C++ STL.

is_ pointer is a template which comes under the <type_traits> header file. This template is used to check whether the given type T is a pointer type or not.

What is a Pointer?

Pointers are non-static types which hold an address of another type or in other words which points to some memory location in the memory pool. With help of an asterisk (*) we define a pointer and when we want to refer the specific memory which the pointer is holding then also we use asterisk(*).

They are the type which can be initialised as null and can later change the type, as per the need.

Syntax

template <class T> is_pod;

Parameters

The template can have only parameter of type T, and check whether the given type is a Pointer or not.

Return value

It returns a Boolean value, true if the given type is a pointer variable, and false if the given type is not a pointer.

Example

Input: is_pointer<int>::value;
Output: False

Input: is_pointer<int*>::value;
Output: True

Example

 Live Demo

#include <iostream>
#include <type_traits>
using namespace std;
class TP{
};
int main() {
   cout << boolalpha;
   cout << "checking for is_pointer:";
   cout << "\nTP: " << is_pointer<TP>::value;
   cout << "\nTP*: " << is_pointer<TP*>::value;
   cout << "\nTP&: " << is_pointer<TP&>::value;
   cout << "\nNull Pointer: "<< is_pointer<nullptr_t>::value;
   return 0;
}

Output

If we run the above code it will generate the following output −

checking for is_pointer:
TP: false
TP*: true
TP&: false
Null Pointer: false

Example

 Live Demo

#include <iostream>
#include <type_traits>
using namespace std;
int main() {
   cout << boolalpha;
   cout << "checking for is_pointer:";
   cout << "\nint: " << is_pointer<int>::value;
   cout << "\nint*: " << is_pointer<int*>::value;
   cout << "\nint **: " << is_pointer<int **>::value;
   cout << "\nint ***: "<< is_pointer<int ***>::value;
   return 0;
}

Output

If we run the above code it will generate the following output −

checking for is_pointer:
int: false
int*: true
Int **: true
Int ***: true
raja
Published on 23-Mar-2020 05:38:09
Advertisements