The Wayback Machine - https://web.archive.org/web/20180314101154/http://ja.cppreference.com:80/w/cpp/language/static_cast
名前空間
変種
操作

static_cast conversion

提供: cppreference.com
< cpp‎ | language

 
 
C++言語
一般的なトピック
フロー制御
条件付き実行文
繰り返し文 (ループ)
ジャンプ文
関数
関数宣言
ラムダ関数宣言
inline 指定子
例外指定(廃止予定)
noexcept 指定子(C++11)
例外
名前空間
指定子
decltype(C++11)
auto(C++11)
alignas(C++11)
記憶域期間指定子
初期化
代替表現
リテラル
ブーリアン - 整数 - 浮動小数点
文字 - 文字列 - nullptr(C++11)
ユーザ定義(C++11)
ユーティリティ
アトリビュート(C++11)
typedef 宣言
型エイリアス宣言(C++11)
キャスト
暗黙の変換 - 明示的な変換
static_cast - dynamic_cast
const_cast - reinterpret_cast
メモリ確保
クラス
クラス固有の関数特性
特別なメンバ関数
テンプレート
その他
 
暗黙およびユーザー定義の変換の組み合わせを使用して、型の間で変換します.
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.

目次

[編集]構文

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.

[編集]説明

のみ、次の変換は、このような変換は 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.
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.
2)
new_typeは、いくつかのクラスDへのポインタまたは参照であり、expressionのタイプは、その非仮想ベースへのポインタまたは参照である場合Bstatic_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.
3)
new_typeは右辺値参照型の場合、static_castexpressionの値を変換するには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.
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.
5)からnew_typeのタイプにexpressionシーケンスが存在する場合、それはその後static_castが実行できる、左辺値から右辺値、配列からポインタ、関数へのポインタ、NULLポインタ、ヌルメンバポインタ、またはブール値変換が含まれていませんその暗黙の型変換の逆.
Original:
If an
暗黙の型変換
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.
</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 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.
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.
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.
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.
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.
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.
すべてのキャスト式と同様に、結果は次のとおりです
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.
  • 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_caststd::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.

[編集]キーワード

static_cast

[編集]

#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

[編集]参照

close