Imagine in C++ two classes one named derived
and another named base
that is a base class of the first. If I had the following code, which is preferred:
base *b = init_value_here;
const derived *d = static_cast<derived *>(b);
or
base *b = init_value_here;
const derived *d = static_cast<const derived *>(b);
In other words, is it better to exclude const
in the static cast when not required since the compiler can promote to constness, or is it better to include it to loosen the restrictions on b
such that it could be made const
in the future more readily?
I strive to do the least number of explicit casts possible, and everything else implicit, if possible.
So I would use the former (i.e not including const
in the cast).
But this is only my opinion. You may be of the opinion to write the const
there too, as it fits your taste.