Hi,
in the code of cppcheck, I see some usage of const_cast to remove const qualifier. For instance, there are some functions like
void foo (const A*a){
const_cast<A *>(a)->modifyA(42); // but foo is not supposed to change a ?!?
}
They seem very wrong to me (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-casts-const). Am I missing something ? Is there any reason for those ?
I'll probably try to fix those if it is ok for everyone.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The checks should only deal with a const version of the token list and tokens. They should not be allowed to modify the internal state of the tokenizer and symbol database.
The tokenizer, symbol database and template simplifier need to edit the internal state but that can be very hard because they have pointers to the internal states (tokens) and changing it invalidates these pointers. We could have private non-const versions of the const functions and use friend to give access.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for the feedback.
I had a quick look, and indeed, some const_cast are rather easy to remove, and some are very complex.
The easy one are due to a missing non const override of some methods, for instance:
Hi,
in the code of cppcheck, I see some usage of const_cast to remove const qualifier. For instance, there are some functions like
They seem very wrong to me (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-casts-const). Am I missing something ? Is there any reason for those ?
I'll probably try to fix those if it is ok for everyone.
I think that would be great, but I am not sure easy it would be to change.
Feel free to change 1 or 2 of those and then show us how you are solving these.. if such cast can be removed cleanly then that is good.
The checks should only deal with a const version of the token list and tokens. They should not be allowed to modify the internal state of the tokenizer and symbol database.
The tokenizer, symbol database and template simplifier need to edit the internal state but that can be very hard because they have pointers to the internal states (tokens) and changing it invalidates these pointers. We could have private non-const versions of the const functions and use friend to give access.
Thanks for the feedback.
I had a quick look, and indeed, some const_cast are rather easy to remove, and some are very complex.
The easy one are due to a missing non const override of some methods, for instance:
That fix seems acceptable. I would like that you create a github pull request for that one.
Sure.
I'll probably remove a bit more const_cast before submitting the pull-request.
https://github.com/danmar/cppcheck/pull/1886