static_cast
has basically the same power and meaning as the general-purpose C-style cast. It also has the same kind of restrictions. For example, you can't cast a struct
into an int
or a double
into a pointer using static_cast
any more than you can with a C-style cast. Furthermore, static_cast
can't remove const
ness from an expression, because another new cast, const_cast
, is designed specifically to do that.
The other new C++ casts are used for more restricted purposes. const_cast
is used to cast away the const
ness or volatile
ness of an expression. By using a const_cast
, you emphasize (to both humans and compilers) that the only thing you want to change through the cast is the const
ness or volatile
ness of something. This meaning is enforced by compilers.