次の方法で共有


safe_cast (C++/CLI および C++/CX)

safe_cast操作は、指定した式を指定した型として返します。 操作が成功しなかった場合は、 InvalidCastExceptionがスローされます。

All Runtimes

(この言語機能にはランタイムに適用される特記事項がありません。)

Syntax

[default]:: safe_cast< type-id >( expression )

Windows ランタイム

safe_castを使用して、指定した式の型を変更します。 If you expect a variable or parameter to be convertible to a certain type, use safe_cast without a try-catch block to detect programming errors during development. For more information, see Casting (C++/CX).

Syntax

[default]:: safe_cast< type-id >( expression )

Parameters

type-id
The type to convert expression to. 参照型または値型へのハンドル、値型、参照型または値型への追跡参照。

expression
参照型または値型へのハンドル、値型、参照型または値型への追跡参照として評価される式。

Remarks

safe_cast throws InvalidCastException if it can't convert expression to the type specified by type-id. To catch InvalidCastException, specify the /EH (Exception Handling Model) compiler option, and use a try/catch statement.

Requirements

コンパイラ オプション: /ZW

Examples

次のコード例は、Windows ランタイムで safe_cast を使用する方法を示しています。

// safe_cast_ZW.cpp
// compile with: /ZW /EHsc

using namespace default;
using namespace Platform;

interface class I1 {};
interface class I2 {};
interface class I3 {};

ref class X : public I1, public I2 {};

int main(Array<String^>^ args) {
   I1^ i1 = ref new X;
   I2^ i2 = safe_cast<I2^>(i1);   // OK, I1 and I2 have common type: X
   // I2^ i3 = static_cast<I2^>(i1);   C2440 use safe_cast instead
   try {
      I3^ i4 = safe_cast<I3^>(i1);   // Fails because i1 is not derived from I3.
   }
   catch(InvalidCastException^ ic) {
   wprintf(L"Caught expected exception: %s\n", ic->Message);
   }
}
Caught expected exception: InvalidCastException

共通言語ランタイム

safe_cast は式の型を変更し、検証可能な MSIL コードを生成します。

Syntax

[cli]:: safe_cast< type-id >( expression )

Parameters

type-id
参照型または値型へのハンドル、値型、参照型または値型への追跡参照。

expression 参照型または値型、値型、または参照型または値型への追跡参照へのハンドルに評価される式。

Remarks

The expression safe_cast<type-id>(expression) converts the operand expression to an object of type type-id.

コンパイラは、safe_castを受け入れるほとんどの場所でstatic_castを受け入れます。 ただし、 safe_cast では検証可能な MSIL が常に生成されますが、 static_cast では検証できない MSIL が生成される場合があります。 検証可能なコードの詳細については、「純粋コードと検証可能コード (C++/CLI)」およびPeverify.exe (PEVerify ツール)」を参照してください。

static_castと同様に、safe_castはユーザー定義の変換を呼び出します。

For more information about casts, see Casting Operators.

safe_cast では、 const_cast は適用されません ( constキャストされます)。

safe_cast は cli 名前空間にあります。 詳細については、「 プラットフォーム、既定、および cli 名前空間」を参照してください。

safe_castの詳細については、以下を参照してください。

Requirements

コンパイラ オプション: /clr

Examples

コンパイラが static_cast を受け入れないが、 safe_cast を受け入れる例の 1 つは、関連のないインターフェイス型間のキャスト用です。 safe_castでは、コンパイラは変換エラーを発行せず、実行時にチェックを実行してキャストが可能かどうかを確認します。

// safe_cast.cpp
// compile with: /clr
using namespace System;

interface class I1 {};
interface class I2 {};
interface class I3 {};

ref class X : public I1, public I2 {};

int main() {
   I1^ i1 = gcnew X;
   I2^ i2 = safe_cast<I2^>(i1);   // OK, I1 and I2 have common type: X
   // I2^ i3 = static_cast<I2^>(i1);   C2440 use safe_cast instead
   try {
      I3^ i4 = safe_cast<I3^>(i1);   // fail at runtime, no common type
   }
   catch(InvalidCastException^) {
      Console::WriteLine("Caught expected exception");
   }
}
Caught expected exception

See also

.NET および UWP でのコンポーネント拡張