How can I remove all const qualifiers from a data type?
I tried to use std::remove_cv but it didnt work.
std::remove_cv< const char *>::type
Isn’t it the same as std::remove_cv<char*>::type?
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The trait is doing everything correctly:
const char*is the same aschar const*and neither is the same aschar* const. So in your case, it’s the pointee that’sconst, not the pointer. Andremove_const(somewhat logically) only removes the outerconst, not inner ones.If you really want to remove the
constness of the pointee, you can do it like this:(Although
std::add_pointer<T>::typecould be dropped in favour of the simplerT*…)That is: remove the pointer, remove the
constof the pointee, make the result a pointer again.In fact, this is a good opportunity to use use R. Martinho Fernandes’ excellent Wheels library which provides convenient shortcuts for such nested traits:
Much more readable.