remove_const (C++)

Difference with const_cast?

const_cast casts a variable from a const type, to a non-const type.

remove_const is an operation done on a type.

Example of where remove_const is used:

template<typename T>
void checkType() {
    typename std::remove_const<T>::type value; // Removes const qualifier
    std::cout << "Is const: " << std::is_const<decltype(value)>::value << "\n";
}
void modifyConst(const int* ptr) {
    int* modifiable = const_cast<int*>(ptr); // Removes constness
    *modifiable = 42; // Modifies the value
}

wait, but can you not do?

void modifyConst(const int* ptr) {
    int* modifiable = std::remove_const<const int*>::type(ptr); // Removes constness
    *modifiable = 42; // Modifies the value
}