名前空間
変種
操作

static_cast conversion

提供: cppreference.com
< cpp‎ | language

 
 
C + +言語
一般的なトピック
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
フロー制御
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
条件付き実行文
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
繰り返し文
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
文をジャンプします
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
機能します
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
関数の宣言
ラムダ関数の宣言
関数テンプレート
の歴史。インライン指定
例外仕様 (廃止予定)
noexcept指定子 (C++11)
例外
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
名前空間
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
指定子
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
CV指定
貯蔵期間指定
constexprの指定子 (C++11)
自動指定 (C++11)
alignas指定子 (C++11)
初期化
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
リテラル
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
代替表現
ユーティリティ
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
型の別名宣言 (C++11)
属性 (C++11)
キャストします
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
暗黙の型変換
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Cスタイルキャストと機能
メモリの割り当て
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス固有の機能特性
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
明示 (C++11)
静的
特殊なメンバ関数
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
テンプレート
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラステンプレート
関数テンプレート
テンプレートの特殊化
パラメーターパック (C++11)
その他
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
インラインアセンブリ
 
暗黙およびユーザー定義の変換の組み合わせを使用して、型の間で変換します.
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 {};
 
enum class E { ONE, TWO, THREE };
enum EU { ONE, TWO, THREE };
 
int main()
{
    // 1: initializing conversion
    int 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 xvalue
    std::vector<int> v2 = static_cast<std::vector<int>&&>(v);
    std::cout << "after move, v.size() = " << v.size() << '\n';
 
    // 4: discarded-value expression
    static_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 type
    void* voidp = &e;
    std::vector<int>* p = static_cast<std::vector<int>*>(voidp);
}

Output:

n = 3
v.size() = 10
after move, v.size() = 0

[編集] も参照してください