Compiler switch to disable const_cast semantics in c-style casts?

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?

+6
source share
1 answer

GCC has the -Wcast-qual option to warn when C-style casting removes the type classifier. In combination with -Werror you can prevent it completely if you want.

+7
source

Source: https://habr.com/ru/post/885826/


All Articles