static_cast conversion
提供: cppreference.com
![]() | このページは、Google 翻訳を使って英語版から機械翻訳されました。 翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
暗黙およびユーザー定義の変換の組み合わせを使用して、型の間で変換します.
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.
目次 |
[編集]構文
static_cast < new_type> ( expression) | |||||||||
タイプ
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.
[編集]説明
のみ、次の変換は、このような変換は const性を捨てたい場合を除き、static_castで行うことができますや'ボラティリティ.
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)
タイプnew_typeの一時オブジェクトが宣言され、など暗黙の型変換、expressionのコンストラクターまたはユーザー定義変換演算子の呼び出しへの呼び出しを含んでもよいnew_type Temp(expression);によって、new_typeで初期化することができれば、その後static_cast<type>(expression)を計算し、その値を返しますその一時オブジェクトの.
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)
new_typeは、いくつかのクラス
D
へのポインタまたは参照であり、expressionのタイプは、その非仮想ベースへのポインタまたは参照である場合B
、static_cast
'ダウンキャストを実行します。このようなstatic_cast
は、オブジェクトの実行時の型が実際にD
であることを保証するために、実行時のチェックを行いませんし、この前提条件がそのような静的ポリモーフィズムを実装する場合など、他の手段によって保証されている場合にのみ安全に使用できます。安全なダウンキャストは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 静的ポリモーフィズム. 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)
new_typeは右辺値参照型の場合、
static_cast
はexpressionの値を変換するにはxValue'。 static_cast
このタイプのstd::moveでmoveセマンティクスを実装するために使用されます。 (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. (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)
new_typeがタイプvoid(多分CV修飾名でも可)であれば、
static_cast
それを評価した後expressionの値を破棄し.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のタイプにexpressionシーケンスが存在する場合、それはその後
static_cast
が実行できる、左辺値から右辺値、配列からポインタ、関数へのポインタ、NULLポインタ、ヌルメンバポインタ、またはブール値変換が含まれていませんその暗黙の型変換の逆.Original:
If an
暗黙の型変換</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)
expressionnew_typeへの変換は左辺値から右辺値、配列からポインタ、または関数へのポインタ変換が含まれている場合、それは
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)
スコープの列挙型は整数型または浮動小数点型に変換することができます。 (C++11およびそれ以降)
Original:
Scoped enumeration type can be converted to an integer or floating-point type. (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)
整数、浮動小数点、または列挙型の任意の列挙型(列挙型の基になる型に変換expressionの値は、ターゲットの列挙値のいずれかでない場合、結果は不定である)に変換することができます
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)
いくつかのクラス
D
のメンバへのポインタは、その基本クラスB
のメンバへのポインタにアップキャストすることができます。このstatic_cast
は、メンバーが実際にポイント先のオブジェクトの実行時の型で存在することを確認するのチェックを行いません.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)
void(多分CV修飾名でも可)へのポインタ型のprvalueは任意の型へのポインタに変換することができます。オリジナル(CV修飾またはそれ以上)の型へのポインタに戻って無効とするためのポインタへのポインタへの変換は、元の値が保持されます.
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.
すべてのキャスト式と同様に、結果は次のとおりです
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.
- new_type場合左辺値は左辺値参照型または型を機能させるには右辺値参照である;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. - new_type場合はxValueは、オブジェクト型への右辺値参照である;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. - そうでなければprvalue.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.
[編集]ノート
static_castも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.
[編集]キーワード
[編集]例
#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);}
出力:
n = 3 v.size() = 10 after move, v.size() = 0