Cannot remove constness from type 'const char*'

Viewed 43

Our goal is to remove the const-ness from the type const char* so that the resulting type is char *. Expected was that this can be done with std::remove_const or std::remove_cv or std::decay.

But this does not work. The documentation for the above operation says that top-level const-ness is removed, so it seems that in the described case the constness is not top-level.

The sample on page https://www.cplusplus.com/reference/type_traits/remove_const/ even documents in the line below that this is not possible:

    std::remove_const<const char*>::type b;    // const char* b

But why is that so?

The program below demonstrates the problem. How can the type ccs_t be transformed to cs_t so that the second test passes?

    #include <iostream>
    #include <type_traits>
 
    int main() {
        using ccs_t = const char*;

        std::cout << "test1 " << (std::is_same<const char*, ccs_t>::value
            ? "passed" : "failed") << '\n';

        using cs_t = std::remove_cv<ccs_t>::type;

        std::cout << "test2 " << (std::is_same<char*, cs_t>::value
            ? "passed" : "failed") << '\n';
    }
0 Answers
Related