|
|
(1人の利用者による、間の2版が非表示) |
1行: |
1行: |
− | {{tr_note}}
| + | {{title|}} |
− | {{title|explicit type conversion}} | + | {{cpp/language/navbar}} |
− | {{cpp/language/navbar}} | + | |
− | {{tr|明示的および暗黙的な変換の組み合わせを使用して、型の間で変換します.|Converts between types using a combination of explicit and implicit conversions.}}
| + | |
| | | |
| ===構文=== | | ===構文=== |
− | {{sdcl list begin}} | + | {{begin}} |
− | {{sdcl list item | {{ttb|(}} {{sparam|new_type}} {{ttb|) }} {{sparam|expression}} | num=1 }} | + | {{| {{ttb|(}} {{|new_type}} {{ttb|) }} {{|expression}} |
− | {{sdcl list item | {{sparam|new_type}} {{ttb|( }} {{sparam|expression}} {{ttb|)}} | num=2 }} | + | | num=}} |
− | {{sdcl list end}} | + | {{| {{|new_type}} {{ttb|( }} {{|}} {{ttb|) }} |
| + | | num=}} |
| + | {{list |
| + | |
| + | |
| + | end}} |
| | | |
− | {{tr|タイプ{{tt|new_type}}の値を返します。.|Returns a value of type {{tt|new_type}}.}}
| + | {{tt|new_type}} |
| | | |
| ===説明=== | | ===説明=== |
− | @1@ {{tr|Cスタイルキャスト式が検出されると、コンパイラは、この順序で、次のキャスト式をしようとします|When the C-style cast expression is encountered, the compiler attempts the following cast expressions, in this order:}} | + | @1@ C |
− | :@a@ {{c|const_cast<new_type>(expression)}} | + | :@a@ {{|const_cast<new_type>(expression)}} |
− | :@b@ {{tr|{{c|static_cast<new_type>(expression)}}、拡張子を持つ:ポインタまたは派生クラスへの参照がさらに基底クラスにアクセスできない場合であっても(つまり、このキャストはprivate継承指定子を無視する)ポインタまたは明白な基本クラスへの参照(およびその逆)にキャストすることを許可されている。同じことは、曖昧でない非仮想ベースのメンバへのポインタをメンバへのポインタをキャストにも適用されます|{{c|static_cast<new_type>(expression)}}, with extensions: pointer or reference to a derived class is additionally allowed to be cast to pointer or reference to unambiguous base class (and vice versa) even if the base class is inaccessible (that is, this cast ignores the private inheritance specifier). Same applies to casting pointer to member to pointer to member of unambigous non-virtual base}} | + | :@b@ {{|{{|static_cast}}{{c|<new_type>(expression)}}derived class(private )pointer}} |
− | :@c@ {{tr|{{c|static_cast}}は(拡張子){{c|const_cast}}続く|{{c|static_cast}} (with extensions) followed by {{c|const_cast}}}} | + | :@c@ {{c|static_cast}} {{c|const_cast}} |
− | :@d@ {{c|reinterpret_cast<new_type>(expression)}}
| + | {{|{{|}}}}{{c|<new_type>(expression)}} |
− | :@e@ {{tr|{{c|reinterpret_cast}}は{{c|const_cast}}続く|{{c|reinterpret_cast}} followed by {{c|const_cast}}}} | + | :@e@ {{c|reinterpret_cast}} {{c|const_cast}} |
− | {{tr|@ @それは(例を参照)コンパイルできない場合でも、それぞれのキャスト演算子の要件を満たす最初の選択は、選択されている.|@@ The first choice that satisfies the requirements of the respective cast operator is selected, even if it cannot be compiled (see example).}} | + | {{|}} {{|const_cast}} |
− | {{tr|@ @また、Cスタイルのキャスト表記は、からの、不完全クラス型へのポインタ間のキャストが許可されます。もし両方{{sparam|expression}}と{{sparam|new_type}}不完全なクラス型へのポインタであり、それは、static_castやreinterpret_castをが選択されるかどうかは不定だ.|@@ In addition, C-style cast notation is allowed to cast from, to, and between pointers to incomplete class type. If both {{sparam|expression}} and {{sparam|new_type}} are pointers to incomplete class types, it's unspecified whether static_cast or reinterpret_cast gets selected.}} | + | }} {{| |
− | @2@ {{tr|機能的なキャストが単純型指定子またはtypedef指定子で構成されています(つまり、単一ワードの種類の名前の:{{c|unsigned int(expression)}}または{{c|int*(expression)}}有効ではありません)、カッコ内の単一の表現が続きます。このキャストにより、対応するCスタイルキャスト式とまったく同じです.|The functional cast consists of a simple type specifier or a typedef specifier (in other words, a single-word type name: {{c|unsigned int(expression)}} or {{c|int*(expression)}} are not valid), followed by a single expression in parentheses. This cast is exactly equivalent to the corresponding C-style cast expression.}} | + | @@ |()}} {{| |
| + | @@ {{|}} {{|}}|{{|}}{{|}}}} |
| + | @@ {{|{{c|}}{{c|() }} |{{|() }} |
| + | {{c|(}}) |
| + | ,-}} |
| | | |
| {{cpp/language/cast_return}} | | {{cpp/language/cast_return}} |
28行: |
36行: |
| {{example | | {{example |
| | code= | | | code= |
− |
| |
| double f = 3.14; | | double f = 3.14; |
− | unsigned int n1 = (unsigned int)f; // C-style cast | + | unsigned int n1 = (unsigned int)f; // C |
− | unsigned int n2 = unsigned(f); // functional cast | + | unsigned int n2 = unsigned(f); // |
| | | |
| class C1; | | class C1; |
37行: |
44行: |
| C2* foo(C1* p) | | C2* foo(C1* p) |
| { | | { |
− | return (C2*)p; // casts incomplete type to incomplete type | + | return (C2*)p; // |
| } | | } |
| | | |
− | // In this example, C-style cast is interpreted as static_cast | + | // C |
− | // even though it would work as reinterpret_cast | + | // |
| struct A {}; | | struct A {}; |
| struct I1 : A {}; | | struct I1 : A {}; |
49行: |
56行: |
| int main() | | int main() |
| { | | { |
− | D* d = nullptr;
| + | D* d = nullptr; |
− | A* a = (A*)d; // compile-time error
| + | A* a = (A*)d; // |
− | A* a = reinterpret_cast<A*>(d); // this compiles
| + | A* a = reinterpret_cast<A*>(d); // |
| } | | } |
| }} | | }} |
| + | |
| + | |
| + | |
| + | |
| + | |
| + | |
| + | |
| + | |
| + | |
| + | |
| + | |
| + | |
| + | |
| + | |
| + | |
| + | |
| + | |
| + | |
| + | |
| + | |
| + | |
| + | |