I recently came across a code, for example:
void foo(const Bar* b) { ... takes_nonconst_param_fn((Bar*)b); ...
Obviously, the developer did not know what he was doing, but if the compiler did not accept the c-style silence and at least demanded proper const_cast
, he could have at least two times before doing this.
So, this made me wonder if modern compilers have a switch to prevent const_cast
semantics for c-style-casts?
It’s just not practical to prevent all occurrences of c-style-casts and it’s a necessary evil to allow their semantics static_
and reinterpret_
(if only for some library code), but my impression is that the legitimate use of c-style-casts is to discard constants very rare in C + + code, so maybe it can be completely disabled?
source share