is_const Template in C++

The std::is_const template of C++ STL is used to check whether the type is a const-qualified or not. It returns a boolean value showing the same.

Syntax:

template  < class T >struct is_const;

Template Parameter: This template contains single parameter T (Trait class) to check whether T is a const-qualified type.

Return Value: This template returns a boolean value as shown below:

  • True: if the type is a const-qualified.
  • False: if the type is a non-const-qualified.

Below programs illustrate the is_const template in C++ STL:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// is_const
  
#include <iostream>
#include <type_traits>
using namespace std;
  
// main program
int main()
{
    cout << boolalpha;
    cout << "is_const:"
         << endl;
    cout << "int:"
         << is_const<int>::value
         << '\n';
    cout << "const int:"
         << is_const<const int>::value
         << '\n';
    cout << "const int&:"
         << is_const<typename remove_reference<const int&>::type>::value
         << '\n';
    return 0;
}

chevron_right


Output:

is_const:
int:false
const int:true
const int&:true

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// is_const
  
#include <iostream>
#include <type_traits>
using namespace std;
  
// main program
int main()
{
    cout << boolalpha;
    cout << "is_const:"
         << endl;
    cout << "const int*:"
         << is_const<const int*>::value
         << '\n';
    cout << "int* const:"
         << is_const<int* const>::value
         << '\n';
    cout << "const int&:"
         << is_const<const int&>::value
         << '\n';
  
    return 0;
}

chevron_right


Output:

is_const:
const int*:false
int* const:true
const int&:false

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// is_const
  
#include <iostream>
#include <type_traits>
using namespace std;
  
// main program
int main()
{
    cout << boolalpha;
    cout << "is_const:"
         << endl;
    cout << "float:"
         << is_const<float>::value
         << '\n';
    cout << "const float:"
         << is_const<const float>::value
         << '\n';
    cout << "char const:"
         << is_const<char const>::value
         << '\n';
    cout << "double:"
         << is_const<double>::value
         << '\n';
  
    return 0;
}

chevron_right


Output:

is_const:
float:false
const float:true
char const:true
double:false


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.