static_cast conversion
De cppreference.com
![]() | This page has been machine-translated from the English version of the wiki using Google Translate. The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Convertis entre les types en utilisant une combinaison de conversions implicites et définie par l'utilisateur .
Original:
Converts between types using a combination of implicit and user-defined conversions.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Sommaire |
[modifier]Syntaxe
static_cast < new_type> ( expression) | |||||||||
Renvoie une valeur de type ..
new_type
Original:
Returns a value of type
new_type
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifier]Explication
Seules les conversions suivantes peut être fait avec static_cast, sauf lorsque de telles conversions jetterait sans constante''' ou la volatilité .
Original:
Only the following conversions can be done with static_cast, except when such conversions would cast away constness or volatility.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
1)
Si un objet temporaire de new_type type peut être déclarée et initialisée avec expression, comme par new_type Temp(expression);, ce qui peut impliquer des conversions implicites, un appel au constructeur de new_type ou d'un appel vers un utilisateur défini par l'opérateur de conversion, puis static_cast<type>(expression) calcule et renvoie la valeur de cet objet temporaire .
Original:
If a temporary object of type new_type can be declared and initialized with expression, as by new_type Temp(expression);, which may involve implicit conversions, a call to the constructor of new_type or a call to a user-defined conversion operator, then static_cast<type>(expression) computes and returns the value of that temporary object.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
2)
Si new_type est un pointeur ou une référence à une certaine
D
classe et le type de expression est un pointeur ou référence à sa non-virtuelle de base B
, static_cast
effectue un' abattu. static_cast
telle ne fait pas de contrôles d'exécution afin de s'assurer que type à l'exécution de l'objet est en fait D
, et ne peuvent être utilisés sans danger si cette condition est assurée par d'autres moyens, tels que la mise en œuvre polymorphisme statique. Abattu sans échec peut être fait avec dynamic_cast .Original:
If new_type is a pointer or reference to some class
D
and the type of expression is a pointer or reference to its non-virtual base B
, static_cast
performs a downcast. Such static_cast
makes no runtime checks to ensure that the object's runtime type is actually D
, and may only be used safely if this precondition is guaranteed by other means, such as when implementing polymorphisme statique. Safe downcast may be done with dynamic_cast.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
3)
Si new_type est un type de référence rvalue,
static_cast
convertit la valeur de expression à' xValue. Ce type de static_cast
est utilisé pour mettre en œuvre la sémantique de déplacement dans std::move. (depuis C++11)Original:
If new_type is an rvalue reference type,
static_cast
converts the value of expression to xvalue. This type of static_cast
is used to implement move semantics in std::move. (depuis C++11)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
4)
Si new_type est le type void (peut-cv-qualifié),
static_cast
ignore la valeur de expression après l'évaluation .Original:
If new_type is the type void (possibly cv-qualified),
static_cast
discards the value of expression after evaluating it.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
5)new_type au type de expression existe, qui ne comprend pas lvalue-à-rvalue, matrice à pointeur, à fonction pointeur, pointeur null, null pointeur élément, ou la conversion booléen, puis
static_cast
peut effectuer l'inverse de ce que la conversion implicite .Original:
If an
conversion implicite</div> sequence from new_type to the type of expression exists, that does not include lvalue-to-rvalue, array-to-pointer, function-to-pointer, null pointer, null member pointer, or boolean conversion, then
Original:
implicit conversion
The text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
static_cast
can perform the inverse of that implicit conversion.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
6)
Si la conversion de expression à new_type implique lvalue-à-rvalue, array-à-pointeur, ou de la fonction à pointeur de conversion, elle peut être réalisée de manière explicite par
static_cast
.Original:
If conversion of expression to new_type involves lvalue-to-rvalue, array-to-pointer, or function-to-pointer conversion, it can be performed explicitly by
static_cast
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
7)
Type d'énumération portée peut être converti en un type entier ou à virgule flottante. (depuis C++11)
Original:
Scoped enumeration type can be converted to an integer or floating-point type. (depuis C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
8)
Integer, type virgule flottante ou une énumération peut être converti en un type d'énumération (le résultat n'est pas spécifié si la valeur de expression, converti en type sous-jacent de l'énumération, n'est pas l'une des valeurs d'énumération cibles)
Original:
Integer, floating-point, or enumeration type can be converted to any enumeration type (the result is unspecified if the value of expression, converted to the enumeration's underlying type, is not one of the target enumeration values)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
9)
Un pointeur vers membre d'une classe peut être
D
upcast à un pointeur à un membre de sa B
classe de base. Cette static_cast
ne fait pas de vérifications pour s'assurer le membre existe réellement dans le type à l'exécution de l'objet pointé à .Original:
A pointer to member of some class
D
can be upcast to a pointer to member of its base class B
. This static_cast
makes no checks to ensure the member actually exists in the runtime type of the pointed-to object.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
10)
Un prvalue de type pointeur void (éventuellement qualifié cv) peut être converti en pointeur de n'importe quel type. La conversion de toute pointeur vers un pointeur d'annuler et revenir au pointeur à l'original (ou plusieurs cv-qualifié) Type conserve sa valeur d'origine .
Original:
A prvalue of type pointer to void (possibly cv-qualified) can be converted to pointer to any type. Conversion of any pointer to pointer to void and back to pointer to the original (or more cv-qualified) type preserves its original value.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Comme pour toutes les expressions de transtypage, le résultat est le suivant:
Original:
As with all cast expressions, the result is:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- une lvalue si new_type est un type de référence lvalue ou une référence rvalue à un type de fonction;Original:an lvalue if new_type is an lvalue reference type or an rvalue reference to function type;The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - un xValue si new_type est une référence rvalue de type d'objet;Original:an xvalue if new_type is an rvalue reference to object type;The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - un prvalue autrement .Original:a prvalue otherwise.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[modifier]Notes
static_cast peuvent également être utilisés pour lever l'ambiguïté des surcharges de fonction en effectuant une conversion de fonction à un pointeur de type spécifique, selon la std::transform(s.begin(), s.end(), s.begin(), static_cast<int(*)(int)>(std::toupper));
Original:
static_cast may also be used to disambiguate function overloads by performing a function-to-pointer conversion to specific type, as in std::transform(s.begin(), s.end(), s.begin(), static_cast<int(*)(int)>(std::toupper));
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifier]Mots-clés
[modifier]Exemple
#include <vector>#include <iostream> struct B {};struct D : B {}; enumclass E { ONE, TWO, THREE };enum EU { ONE, TWO, THREE }; int main(){// 1: initializing conversionint n =static_cast<int>(3.14);std::cout<<"n = "<< n <<'\n';std::vector<int> v =static_cast<std::vector<int>>(10);std::cout<<"v.size() = "<< v.size()<<'\n'; // 2: static downcast D d; B& br = d;// upcast via implicit conversion D& another_d =static_cast<D&>(br);// downcast // 3: lvalue to xvaluestd::vector<int> v2 =static_cast<std::vector<int>&&>(v);std::cout<<"after move, v.size() = "<< v.size()<<'\n'; // 4: discarded-value expressionstatic_cast<void>(v2.size()); // 5. inverse of implicit conversion// todo // 6. array-to-pointer followed by upcast D a[10]; B* dp =static_cast<B*>(a); // 7. scoped enum to int or float E e = E::ONE;int one =static_cast<int>(e); // 8. int to enum, enum to another enum E e2 =static_cast<E>(one); EU eu =static_cast<EU>(e2); // 9. pointer to member upcast// todo // 10. void* to any typevoid* voidp =&e;std::vector<int>* p =static_cast<std::vector<int>*>(voidp);}
Résultat :
n = 3 v.size() = 10 after move, v.size() = 0