Explicit type conversion
Converts between types using a combination of explicit and implicit conversions.
Contents |
[edit] Syntax
( new_type ) expression
|
(1) | ||||||||
new_type ( expression )
|
(2) | ||||||||
new_type ( arg1, arg2, ... )
|
(3) | ||||||||
new_type ( )
|
(4) | ||||||||
new_type { arg1, arg2, ...(optional) }
|
(5) | (since C++11) | |||||||
template-name ( arg1, arg2, ...(optional) )
|
(6) | (since C++17) | |||||||
template-name { arg1, arg2, ...(optional) }
|
(7) | (since C++17) | |||||||
Returns a value of type new_type.
[edit] Explanation
const_cast<new_type>(expression);static_cast<new_type>(expression), with extensions: pointer or reference to a derived class is additionally allowed to be cast to pointer or reference to unambiguous base class (and vice versa) even if the base class is inaccessible (that is, this cast ignores the private inheritance specifier). Same applies to casting pointer to member to pointer to member of unambiguous non-virtual base;reinterpret_cast<new_type>(expression);static_cast followed by a const_cast, it cannot be compiled.void, the expression is a void prvalue without a result object (since C++17).void, the expression is a void prvalue without a result object (since C++17).
|
This is the only cast expression that can create an array prvalue. |
(until C++20) |
As with all cast expressions, the result is:
- an lvalue if new_type is an lvalue reference type or an rvalue reference to function type;
- an xvalue if new_type is an rvalue reference to object type;
- a prvalue otherwise.
[edit] Example
double f = 3.14; unsigned int n1 = (unsigned int)f; // C-style cast unsigned int n2 = unsigned(f); // functional cast class C1; class C2; C2* foo(C1* p) { return (C2*)p; // casts incomplete type to incomplete type } // In this example, C-style cast is interpreted as static_cast // even though it would work as reinterpret_cast struct A {}; struct I1 : A {}; struct I2 : A {}; struct D : I1, I2 {}; int main() { D* d = nullptr; // A* a = (A*)d; // compile-time error A* a = reinterpret_cast<A*>(d); // this compiles }
[edit] References
- C++11 standard (ISO/IEC 14882:2011):
- 5.2.3 Explicit type conversion (functional notation) [expr.type.conv]
- 5.4 Explicit type conversion (cast notation) [expr.cast]
- C++98 standard (ISO/IEC 14882:1998):
- 5.2.3 Explicit type conversion (functional notation) [expr.type.conv]
- 5.4 Explicit type conversion (cast notation) [expr.cast]
[edit] See also
| const_cast conversion | adds or removes const |
| static_cast conversion | performs basic conversions |
| dynamic_cast conversion | performs checked polymorphic conversions |
| reinterpret_cast conversion | performs general low-level conversions |
| standard conversions | implicit conversions from one type to another |
| C documentation for cast operator
| |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
