Stdis Pointer Template In C++

In this article, we will discuss the std::ispointer template in C++ with its syntax, parameters, and examples. Before discussing the ispointer template, we must know about the pointers.

What are Pointers?

The memory address of an object is stored in a variable called a pointer. Pointers are addresses represented symbolically. They allow programs to make and function with dynamic data structures , and they allow them to replicate call-by-reference.

In both C and C++ , pointers are frequently used for three primary purposes:

  • To iterate over elements in arrays or other data structures.
  • To allocate new objects on the heap.
  • To pass functions to other functions.
  • Syntax:

It has the following syntax:

Example

datatype   *var_name;

Example: int *p;

Where p can point to the address, which holds int data.

Example:

Let us take an example to illustrate the pointers in C++ .

Example

#include <iostream>
using namespace std;
int main()
{
    int var = 20;
    int *ptr;
    ptr = &var;
    cout << "Initial value of var is: " << var << endl;
    cout << "Initial value of *ptr is: " << *ptr << endl;   
    // changing the value of var using ptr
    *ptr = 55;
    cout << "New value of *ptr is: " << *ptr << endl;
    cout << "New value of var is: " << var << endl;
    return 0;
}

Output:

What is the std::is_pointer?

In C++ , the type variable std::ispointer is specified in the <typetraits> header of the standard library. It is used to determine whether a particular method is a pointer type or not. This variant provides a Boolean value member constant that is true if the provided type is true otherwise it provides false.

Syntax:

It has the following syntax:

Example

template <class T > struct is_pointer;

This template accepts a parameter T (Trait class) to specify whether T is a pointer or not.

This template returns Boolean values as follows,

  • If the type is a pointer, it returns true.
  • If the type is not a pointer, it returns false.
  • Pseudocode:

    Example
    
    template <typename T>
    struct is_pointer {
        static constexpr bool value = /* some logic to determine if T is a pointer */;
    };
    // Specialization for pointer types
    template <typename U>
    struct is_pointer<U*> {
        static constexpr bool value = true;
    };
    // Specialization for non-pointer types
    template <typename V>
    struct is_pointer<V> {
        static constexpr bool value = false;
    };
    

    Example 1:

Let us take an example to illustrate the std::is_pointer in C++.

Example

#include <iostream>
#include <type_traits>
class A {};
int main() {
    std::cout << std::boolalpha;
    std::cout << std::is_pointer<A>::value << '\n';
    std::cout << std::is_pointer<A *>::value << '\n';
    std::cout << std::is_pointer<A &>::value << '\n';
    std::cout << std::is_pointer<int>::value << '\n';
    std::cout << std::is_pointer<int *>::value << '\n';
    std::cout << std::is_pointer<int **>::value << '\n';
    std::cout << std::is_pointer<int[10]>::value << '\n';
    std::cout << std::is_pointer<std::nullptr_t>::value << '\n';
}

Output:

Example 2:

Let us take another example to illustrate the std::is_pointer in C++.

Example

#include <iostream> 
#include <type_traits> 
using namespace std; 
int main() 
{ 
	cout << boolalpha; 
	cout << "is_pointer:" << endl; 
	cout << "int *&: "
		<< is_pointer<int*&>::value << '\n'; 
	cout << "int *[1]: "
		<< is_pointer<int * [1]>::value << '\n'; 
	cout << "float *: "
		<< is_pointer<float*>::value << '\n'; 
	cout << "int[1]:"
		<< is_pointer<int[1]>::value << '\n'; 
	return 0; 
}

Output:

Input Required

This code uses input(). Please provide values below: