Converts between types using a combination of implicit and user-defined conversions.
[edit] Syntaxstatic_cast<
target-type >(
expression )
Returns a value of type target-type.
[edit] ExplanationOnly the following conversions can be done with static_cast, except when such conversions would cast away constness (or volatility).
1)If
expressionis an lvalue of type â
cv1Base
â and
target-typeis âreference to
cv2Derived
â, the result refers to the object of type
Derived
enclosing
expressionif all following conditions are satisfied:
Derived
is a complete class type.Base
is a base class of Derived
.If any of the following conditions is satisfied, the program is ill-formed:
Base
is a virtual base class of Derived
.Base
is a base class of a virtual base class of Derived
.Derived
â to âpointer to Base
â exists.If
expressionis actually not a base class subobject of an object of type
Derived
, the behavior is undefined.
struct B {}; struct D : B { B b; }; D d; B& br1 = d; B& br2 = d.b; static_cast<D&>(br1); // OK, lvalue denoting the original âdâ object static_cast<D&>(br2); // UB: the âbâ subobject is not a base class subobject
2) If target-type is ârvalue reference to Derived
â and expression is an xvalue of type â(possibly cv-qualified) Base
â such that Base
is a base class of Derived
, the result and constraints of such a conversion are the same as those of the âBase
lvalue to Derived
referenceâ conversion.
If
target-typeis an rvalue reference type and the referenced type is
reference-compatiblewith the type of
expression,
static_castconverts the value of
glvalue, class prvalue, or array prvalue(until C++17)any lvalue(since C++17) expressionto xvalue referring to the same object as the expression, or to its base class subobject (depending on
target-type).
[1]If target-type is an inaccessible or ambiguous base of the type of expression, the program is ill-formed.
If
expressionis a
bit-fieldlvalue, it is first converted to prvalue of the underlying type.
(since C++11) 4)If
target-typeis the (possibly cv-qualified)
void, the conversion has no result. In this case,
expressionis a
discarded-value expression.
5)Otherwise,
expressioncan be explicitly converted to
target-typeif
the declaration target-type temp(expression ); is well-formed for some invented temporary variable temp.
The effect of such an explicit conversion is the same as performing the declaration and initialization and then using temp as the result of the conversion. The expression is used as an lvalue(until C++11)a glvalue(since C++11) if and only if the initialization uses it as an lvalue(until C++11)a glvalue(since C++11).
(until C++17)any of the following conditions is satisfied:
The explicit conversion is defined as follows:
If a program uses static_cast to perform the inverse of an ill-formed standard conversion sequence, it is ill-formed.
7) Otherwise, lvalue-to-rvalue, array-to-pointer, and function-to-pointer conversions are applied to expression. After these conversions, only the following conversions can be performed by static_cast:
a)A value of
scoped enumerationtype can be converted to an integer or floating-point type.
A value of integer or enumeration type can be converted to any complete enumeration type.
A value of a floating-point type can also be converted to any complete enumeration type. The result is the same as
convertingthe original value of
expressionfirst to the underlying type of
target-type, and then to
target-typeitself.
d)A prvalue of floating-point type can be explicitly converted to any other floating-point type.
of type âpointer to
cv1Base
â can be explicitly converted to the type âpointer to
cv2Derived
â if all following conditions are satisfied:
Derived
is a complete class type.Base
is a base class of Derived
.If
expressionis a
null pointer value, the result is a null pointer value of type
target-type. Otherwise, the result is a pointer to the object of type
Derived
enclosing the object of type
Base
pointed to by
expression.
If any of the following conditions is satisfied, the program is ill-formed:
Base
is a virtual base class of Derived
.Base
is a base class of a virtual base class of Derived
.Derived
â to âpointer to Base
â exists. If expression is not a null pointer value and does not actually point to a base class subobject of an object of type Derived
, the behavior is undefined.
of type âpointer to member of
Derived
of type
cv1T
â can be explicitly converted to the type âpointer to member of
Base
of type
cv2T
â if all following conditions are satisfied:
Derived
is a complete class type.Base
is a base class of Derived
. If expression is a null member pointer value, the result is a null member pointer value of type target-type. Otherwise, the result is a pointer to the original (possibly indirect) member of class Base
.
If no valid standard conversion from âpointer to member of Base
of type T
â to âpointer to member of Derived
of type T
â exists, the program is ill-formed.
If expression is not a null member pointer value and the member it denotes is not a (possibly indirect) member of class Base
, the behavior is undefined.
of type âpointer to
cv1 voidâ can be explicitly converted to the type âpointer to
cv2T
â if
T
is an object type and
cv1is not a greater cv-qualification than
cv2.
A
of a byte in memory and A
satisfies the alignment requirement of T
, then the resulting pointer value also represents A
.T
â, the result has the original value.A
of a byte in memory but A
does not satisfy the alignment requirement of T
, then the resulting pointer value is unspecified.T
(ignoring cv-qualification) that is pointer-interconvertible (see below) with a, the result is a pointer to b.As with all cast expressions, the result is:
Two objects a and b are pointer-interconvertible if:
union U { int a; double b; } u; void* x = &u; // x's value is âpointer to uâ double* y = static_cast<double*>(x); // y's value is âpointer to u.bâ char* z = static_cast<char*>(x); // z's value is âpointer to uâ[edit] Notes
Base-to-derived conversions (downcasts) using static_cast make no runtime checks to ensure that the dynamic type of the pointed/referred object is Derived
, and may only be used safely if this precondition is guaranteed by other means, such as when implementing static polymorphism. Safe downcast may be done with dynamic_cast
.
static_cast may also be used to disambiguate function overloads by performing a function-to-pointer conversion to specific type, as in
[edit] Keywords [edit] Example#include <iostream> #include <vector> struct B { int m = 42; const char* hello() const { return "Hello world, this is B!\n"; } }; struct D : B { const char* hello() const { return "Hello world, this is D!\n"; } }; enum class E { ONE = 1, TWO, THREE }; enum EU { ONE = 1, TWO, THREE }; int main() { // 1. static downcast D d; B& br = d; // upcast via implicit conversion std::cout << "1) " << br.hello(); D& another_d = static_cast<D&>(br); // downcast std::cout << "1) " << another_d.hello(); // 3. lvalue to xvalue std::vector<int> v0{1, 2, 3}; std::vector<int> v2 = static_cast<std::vector<int>&&>(v0); std::cout << "3) after move, v0.size() = " << v0.size() << '\n'; // 4. discarded-value expression static_cast<void>(v2.size()); // 5. initializing conversion int n = static_cast<int>(3.14); std::cout << "5) n = " << n << '\n'; std::vector<int> v = static_cast<std::vector<int>>(10); std::cout << "5) v.size() = " << v.size() << '\n'; // 6. inverse of implicit conversion void* nv = &n; int* ni = static_cast<int*>(nv); std::cout << "6) *ni = " << *ni << '\n'; // 7a. scoped enum to int E e = E::TWO; int two = static_cast<int>(e); std::cout << "7a) " << two << '\n'; // 7b. int to enum, enum to another enum E e2 = static_cast<E>(two); [[maybe_unused]] EU eu = static_cast<EU>(e2); // 7f. pointer to member upcast int D::*pm = &D::m; std::cout << "7f) " << br.*static_cast<int B::*>(pm) << '\n'; // 7g. void* to any object pointer void* voidp = &e; [[maybe_unused]] std::vector<int>* p = static_cast<std::vector<int>*>(voidp); }
Output:
1) Hello world, this is B! 1) Hello world, this is D! 3) after move, v0.size() = 0 5) n = 3 5) v.size() = 10 6) *ni = 3 7a) 2 7f) 42[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior CWG 137 C++98 the constness and volatility ofRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4