How to use std::remove_const in C ++

In this post, we are going to learn about the standard function defined inside STL(standard template library) from <type_traits> header file std::remove_const. The std::remove_const returns type without const qualified. Const qualified is the term in C++ where you can apply a const keyword before or after the type. So neither value of a pointed variable may change nor the address of that pointed variable.


template <class New>
call function:

The template takes only a single parameter i.e Trait class to check wheater Trait class is using a const qualifier. It returns the boolean value depending upon the trait class whether it consists of a const qualifier. If so, it returns a true value if not it returns a false value.

using namespace std; 
int main() 

    typedef std::remove_const<int>::type first_variable; 
    typedef std::remove_const<int* const>::type second_variable;
    cout<<"Does first_variable contain const int? "
        <<is_same<int, first_variable>::value 
    cout<<"Does second_variable contain const int? "
        <<is_same<int, second_variable>::value 
    return 0; 

In the above program, we have included <type_traits> header file so that our std::remove_const can be explored. then we have used the typedef method and called an std::remove_const method over int type over first_variable thus it should return a true value.

similarly, we have called remove_const over int* const for type second_variable and the value of both address pointer cannot be changed and returns false bool value.

Thus this standard template library from type_traits header file returns the True value for the function which does not have const qualifiers and False if it has the const qualifiers.


Does first_variable contain const int? true
Does second_variable contain const int? false

Also read: Iterator library in C++ STL

Leave a Reply

Your email address will not be published. Required fields are marked *