{{Short description|C++ type conversion operator}} {{DISPLAYTITLE:static_cast}} In the C++ programming language, <code>static_cast</code> is an operator that performs an explicit type conversion.<ref>{{cite book|title=Programming: Principles and Practice Using C++ |year=2009 |publisher=Addison-Wesley |location=New Jersey, US |isbn=978-0321543721 |page=594 |oclc=988823060 }}<!-- Originally said year=2008, but there does not seem to be a 2008 edition, and the ISBN consistently matches the 2009 records at OCLC etc. --></ref>

==Syntax== <syntaxhighlight lang=cpp> static_cast<type> (object); </syntaxhighlight>

The ''type'' parameter must be a data type to which ''object'' can be converted via a known method, whether it be a builtin or a cast. The type can be a reference or an enumerator. All types of conversions that are well-defined and allowed by the compiler are performed using <code>static_cast</code>.<ref>{{cite book |last=Eckel |first=Bruce |title=Thinking in C++ |year=2000 |publisher=Prentice Hall |location=New Jersey, US |isbn=0-13-979809-9 |page=857 |url-access=registration |url=https://archive.org/details/thinkinginc00ecke/page/857 }}</ref>{{Failed verification |date=July 2020 |reason=There is no page 857 in this 814-page book. Is this the right edition?}}

The <code>static_cast<></code> operator can be used for operations such as: * converting a pointer of a base class to a pointer of a non-virtual derived class (downcasting); * converting numeric data types such as enums to ints or floats.

Although <code>static_cast</code> conversions are checked at compile time to prevent obvious incompatibilities, no run-time type checking is performed that would prevent a cast between incompatible data types, such as pointers. A <code>static_cast</code> from a pointer to a class <code>B</code> to a pointer to a derived class <code>D</code> is ill-formed if <code>B</code> is an inaccessible or ambiguous base of <code>D</code>. A <code>static_cast</code> from a pointer of a virtual base class (or a base class of a virtual base class) to a pointer of a derived class is ill-formed.

==See also== * dynamic_cast * reinterpret_cast * const_cast

==References== {{Reflist}}

{{DEFAULTSORT:Static Cast}} Category:C++ Category:Type theory Category:Articles with underscores in the title