A C++ keyword, one of the new-style casts introduced into the C++ type system. It converts values of one type to another when that conversion is well-defined.

Given an expression e and a type T, static_cast<T>(e) will work if:

  • an object of type T can be constructed with e. That is, if the declaration T t(e); is valid.
  • if T is void(the value goes away).
  • e's value has a class type derived from T. That is, you can cast from a derived type back any of its ancestor types.
  • You can do the reverse of any implicit conversion. static_cast, is how you convert between any enumerated type and int.
  • Some pointers to base types can be cast to their derived types, and pointers to member functions of derived types can be cast to pointers to corresponding member functions of that type's base type.
  • Finally, you can cast a void * to any other pointer type.

Most of the casts used in C are candidates for static_cast in C++. They are deliberately made to look ugly so that they will be used infrequently. Since C-style casts are deprecated in C++, you should use static_cast in any new C++ code you write instead.

Log in or register to write something here or to contact authors.