How to remove the const from 'char const*'
c++, c++11
Solution
If you want to remove all const qualifiers you need a solution that recursively removes const from all levels:
template<typename T> struct remove_all_const : std::remove_const<T> {};
template<typename T> struct remove_all_const<T*> {
typedef typename remove_all_const<T>::type *type;
};
template<typename T> struct remove_all_const<T * const> {
typedef typename remove_all_const<T>::type *type;
};
int main() {
std::cout << typeid(remove_all_const<int const * * const>::type).name() << '\n';
}
Problem
It appears that std::remove_const isn't able to remove the const-ness of `const char*`. Consider the following code: ``` #include <iostream> #include <type_traits> #include <typeinfo> template< typename T > struct S { static void foo( ) { std::cout << typeid(T).name() << std::endl; std::cout << typeid( std::remove_const<T>::type ).name() << std::endl; } }; int main( ) { S<char const*>::foo(); } ``` Output of this program (on Visual Studio 2010): ``` char const * char const * ``` And in gcc we have the readable output (code here): ``` PKc PKc ``` I would hope to get `char *` on the second line of Microsoft compiler, and whatever (but different than 1st line) on gcc. What am I doing wrong? How do I turn `char const*` to `char*`?