名前空間
変種
操作

reinterpret_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 by reinterpreting the underlying bit pattern.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目次

[編集] 構文

reinterpret_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.

[編集] 説明

static_castとは異なりますが、const_castように、reinterpret_cast式は、任意のCPU命令にコンパイルされません。それは純粋にそれがタイプexpressionを持っているかのようnew_typeのビット(オブジェクト表現)のシーケンスを処理するようにコンパイラに指示コンパイラディレクティブです.
Original:
Unlike static_cast, but like const_cast, the reinterpret_cast expression does not compile to any CPU instructions. It is purely a compiler directive which instructs the compiler to treat the sequence of bits (object representation) of expression as if it had the 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性を捨てたい場合を除き、reintepret_castで行うことができますや'ボラティリティ.
Original:
Only the following conversions can be done with reintepret_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)
一体型、列挙型、ポインタ、メンバへのポインタ型の式は、独自の型に変換することができます。結果の値はexpressionの値と同じです。 (C + + 11以来)
Original:
An expression of integral, enumeration, pointer, or pointer-to-member type can be converted to its own type. The resulting value is the same as the value of expression. (C + + 11以来)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
任意のポインタはポインタの値(std::uintptr_tへEG)を保持するのに十分な大きさの任意の整数型に変換することができます
Original:
Any pointer can be converted to any integral type large enough to hold the value of the pointer (e.g. to std::uintptr_t)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
任意の整数型または列挙型の値をポインタ型に変換することができます。十分な大きさとバック同じポインタ型の整数に変換するポインタは他の方法で結果のポインタを安全に参照することはできません、元の値を持つことが保証されています。 nullポインタ定数NULLまたは整数のゼロは、ターゲット型のヌルポインタの値を生成するために保証されていません。static_castまたは<div class="t-tr-text">暗黙の型変換
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.
は、この目的のために使用されるべきである..
Original:
A value of any integral or enumeration type can be converted to a pointer type. A pointer converted to an integer of sufficient size and back to the same pointer type is guaranteed to have its original value, otherwise the resulting pointer cannot be dereferenced safely. The null pointer constant NULL or integer zero is not guaranteed to yield the null pointer value of the target type; static_cast or
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
4)
std::nullptr_t含むタイプnullptrの任意の値は、それが(void*)0であるかのように、任意の整数型に変換することができますが、値があっても、nullptrstd::nullptr_tに変換することができないではない。static_castその目的のために使用されるべきである。 (C + + 11以来)
Original:
Any value of type std::nullptr_t, including nullptr can be converted to any integral type as if it was (void*)0, but no value, not even nullptr can be converted to std::nullptr_t: static_cast should be used for that purpose. (C + + 11以来)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
5)
タイプT1のオブジェクトへのポインタが別の型T2のオブジェクトへのポインタに変換することができます。 T2の整列要件がT1のより厳格でない場合は、元の型に戻して結果のポインタの変換は結果のポインタを安全に参照することはできませんそれ以外の場合は、元の値が得られます。 タイプのエイリアシングルール(下記参照)で許可されていれば、どのような場合では、得られたポインタは、安全にデリファレンスすることができます
Original:
Any pointer to object of type T1 can be converted to pointer to object of another type T2. If T2's alignment requirement is not stricter than T1's, conversion of the resulting pointer back to its original type yields the original value, otherwise the resulting pointer cannot be dereferenced safely. In any case, the resulting pointer may only be dereferenced safely if allowed by the type aliasing rules (see below)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
6)
タイプT1の左辺値式は、別のタイプのT2への参照に変換することができます。結果は左辺値または元の左辺値としてではなく、異なるタイプを持つ同じオブジェクトを参照するのxValueです。も一時は作成されず、コピーは行われません、何のコンストラクタまたは変換関数は呼び出されません。 タイプのエイリアシングルール(下記参照)によって許可された場合、結果の参照が唯一安全にアクセスすることができます
Original:
An lvalue expression of type T1 can be converted to reference to another type T2. The result is an lvalue or xvalue referring to the same object as the original lvalue, but with a different type. No temporary is created, no copy is made, no constructors or conversion functions are called. The resulting reference can only be accessed safely if allowed by the type aliasing rules (see below)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
7)
関数へのポインタは、別の関数型へのポインタに変換できます。異なる関数型へのポインタを介して関数を呼び出すと、定義されていませんが、元の関数型へのポインタに戻すようなポインタに変換すると、元の関数へのポインタが得られ.
Original:
Any pointer to function can be converted to a pointer to a different function type. Calling the function through a pointer to a different function type is undefined, but converting such pointer back to pointer to the original function type yields the pointer to the original function.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
8)
(具体的には、すべてのPOSIX互換システム上の)いくつかの実装では、関数ポインタはオブジェクトポインタ、またはその逆に変換することができます。実装は両方向、元の型への変換では、変換をサポートしている場合は、元の値が得られ、それ以外の結果のポインタを間接参照したり安全に呼び出すことができません.
Original:
On some implementations (in particular, on any POSIX compatible system), a function pointer can be converted to an object pointer or vice versa. If the implementation supports conversion in both directions, conversion to the original type yields the original value, otherwise the resulting pointer cannot be dereferenced or called safely.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
9)
任意のポインタ型のヌルポインタの値は、その型のヌルポインタ値で、その結果、他のポインタ型に変換することができます。暗黙的な変換またはnullptrは、この目的のために使用されるべきである..:nullポインタ定数std::nullptr_tまたはタイプreinterpret_castのその他の値がstatic_cast付きのポインタに変換できないことに注意してください
Original:
The null pointer value of any pointer type can be converted to any other pointer type, resulting in the null pointer value of that type. Note that the null pointer constant nullptr or any other value of type std::nullptr_t cannot be converted to a pointer with reinterpret_cast: implicit conversion or static_cast should be used for this purpose.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
10)
メンバ関数への右辺値のポインタは、別の型の異なるメンバ関数へのポインタに変換することができます。元の型への変換は、元の値が得られ、それ以外の結果のポインタを安全に使用することができません.
Original:
An rvalue pointer to member function can be converted to pointer to a different member function of a different type. Conversion to the original type yields the original value, otherwise the resulting pointer cannot be used safely.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
11)
いくつかのクラスT1のメンバオブジェクトへの右辺値のポインタが別のクラスT2の別のメンバーオブジェクトへのポインタに変換することができます。 T2のアライメントがT1のより厳格でない場合は、元の型への変換は結果のポインタを安全に使用することができませんそうでなければ、元の値が得られます.
Original:
An rvalue pointer to member object of some class T1 can be converted to a pointer to another member object of another class T2. If T2's alignment is not stricter than T1's, conversion to the original type yields the original value, otherwise the resulting pointer cannot be used safely.
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.

[編集] キーワード

reinterpret_cast

[編集] エイリアシングを入力します

場合、異なるタイプT1のオブジェクトへのポインタまたは参照型のオブジェクトへのポインタまたは参照reintrepret_castですT2(またはCスタイルキャスト)、キャストは常に成功しますが、結果のポインタまたは参照のみのいずれかの場合にアクセスすることができる次のことが言えます
Original:
When a pointer or reference to object of type T1 is reintrepret_cast (or C-style cast) to a pointer or reference to object of a different type T2, the cast always succeeds, but the resulting pointer or reference may only be accessed if one of the following is true:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • T2は、オブジェクトの(おそらくCV修飾名でも可)ダイナミック型です
    Original:
    T2 is the (possibly cv-qualified) dynamic type of the object
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T2とT1は両方の(おそらくマルチレベル、おそらく各レベルでのcv-修飾)と同じタイプのT3(C + + 11以来)へのポインタです
    Original:
    T2 and T1 are both (possibly multi-level, possibly cv-qualified at each level) pointers to the same type T3 (C + + 11以来)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T2は、オブジェクトの動的な型の(ことによるCV修飾)、符号付きまたは符号なしの変形です
    Original:
    T2 is the (possibly cv-qualified) signed or unsigned variant of the dynamic type of the object
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • これは、それが安全にキャストすることができます:T2は集約型または要素または非静的メンバ(再帰的に、subaggregatesと含まれている労働組合の非静的データメンバの要素を含む)のような上記のタイプのいずれかを保持しているユニオン·タイプです構造体の最初のメンバーから、共用体の要素からそれを含む構造体/共用体へ.
    Original:
    T2 is an aggregate type or a union type which holds one of the aforementioned types as an element or non-static member (including, recursively, elements of subaggregates and non-static data members of the contained unions): this makes it safe to cast from the first member of a struct and from an element of a union to the struct/union that contains it.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T2は、オブジェクトの動的な型の(ことによるCV修飾名でも可)の基底クラスです
    Original:
    T2 is a (possibly cv-qualified) base class of the dynamic type of the object
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T2 is char or unsigned char
T2は、これらの要件を満たしていない場合、新しいポインタまたは参照を介してオブジェクトにアクセスすると、未定義の動作を呼び出します。これは、厳密な別名'ルールとして知られており、両方のC + +とCのプログラミング言語に適用されます.
Original:
If T2 does not satisfy these requirements, accessing the object through the new pointer or reference invokes undefined behavior. This is known as the strict aliasing rule and applies to both C++ and C programming languages.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]

reinterpret_castのいくつかの用途を示しています
Original:
Demonstrates some uses of reinterpret_cast:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <cstdint>
#include <cassert>
#include <iostream>
int f() { return 42; }
int main()
{
    int i = 7;
 
    // pointer to integer and back
    uintptr_t v1 = reinterpret_cast<uintptr_t>(&i); // static_cast is an error
    std::cout << "The value of &i is 0x" << std::hex << v1 << '\n';
    int* p1 = reinterpret_cast<int*>(v1);
    assert(p1 == &i);
 
    // pointer to function to another and back
    void(*fp1)() = reinterpret_cast<void(*)()>(f);
    // fp1(); undefined behavior
    int(*fp2)() = reinterpret_cast<int(*)()>(fp1);
    std::cout << std::dec << fp2() << '\n'; // safe
 
    // type aliasing through pointer
    char* p2 = reinterpret_cast<char*>(&i);
    if(p2[0] == '\x7')
        std::cout << "This system is little-endian\n";
    else
        std::cout << "This system is big-endian\n";
 
    // type aliasing through reference
    reinterpret_cast<unsigned int&>(i) = 42;
    std::cout << i << '\n';
}

Output:

The value of &i is 0x7fff352c3580
42
This system is little-endian
42

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

const_castを変換
追加またはconst削除されます
Original:
adds or removes const
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[edit]
static_castに変換
基本的な変換を行います
Original:
performs basic conversions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[edit]
dynamic_castを変換
チェックポリモーフィックな変換を行います
Original:
performs checked polymorphic conversions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[edit]