is_const Template in C++

C++Server Side ProgrammingProgramming

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

is_const template in C++ is used to check whether the defined type is a const-qualified type or not.

What is const-qualified type?

We say a type as a const-qualified when the value of the type is constant. Constant data type is a type in which once a value is initialised in a const can’t be changed or altered throughout the program.

Syntax

template <class T> is_const;

Parameters

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

Return value

It returns a Boolean value, true if the given type is a const-qualifier, and false if the given type is not a const-qualifier.

Example

Input: is_const<const int>::value;
Output: True
Input: is_const<int>::value;
Output: False

Example

 Live Demo

#include <iostream>
#include <type_traits>
using namespace std;
int main() {
   cout << boolalpha;
   cout << "checking for is_const template: ";
   cout << "\nInt : "<<is_const<int>::value;
   cout << "\nConst int : "<< is_const<const int>::value;
   cout << "\nConst int& : "<< is_const<const int&>::value;
   return 0;
}

Output

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

checking for is_const template:
Int : false
Const int : true
Const int& : false

Example

 Live Demo

#include <iostream>
#include <type_traits>
using namespace std;
int main() {
   cout << boolalpha;
   cout << "checking for is_const template: ";
   cout << "\nFloat : "<<is_const<float>::value;
   cout << "\nChar : "<<is_const<char>::value;
   cout << "\nFloat *: "<<is_const<float*>::value;
   cout << "\nChar *: "<<is_const<char*>::value;
   cout << "\nConst int* : "<< is_const<const int*>::value;
   cout << "\nint* const : "<< is_const<int* const>::value;
   return 0;
}

Output

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

checking for is_const template:
Float : false
Char: false
Float *: false
Char *: fakse
Const int* : false
int* const: true
raja
Published on 23-Mar-2020 07:03:10
Advertisements