次の方法で共有


型の特徴のコンパイラ サポート (C++ コンポーネント拡張)

コンパイラ サポートにより、コンパイル時に型のさまざまな特性を示す 特徴を入力します。

すべてのランタイム

解説

型の特徴は、ライブラリを記述するプログラマに特に便利です。

次の表は、コンパイラによってサポートされる型の特徴を示します。 すべての型の特徴は型の特徴の名前で指定された条件を満たす false を返します。

(テーブルの説明列に、コード例は C++/CLIだけに書き込まれます。 それに対応する型の特徴は、Visual C++ コンポーネント拡張 で特に明記されていない限り、サポートされます。 用語は、プラットフォーム「型」 Windows ランタイム の型または共通言語ランタイム型を示します)。

型の特徴

説明

__has_assign(type)

プラットフォームにまたはネイティブ型はコピー代入演算子がある場合に true を返します。

// has_assign.cpp
// compile with: /clr
ref struct R {
   void operator=(R% r) {}
};
int main() {
   System::Console::WriteLine(__has_assign(R));
}

__has_copy(type)

プラットフォームにまたはネイティブ型でコピー コンストラクターがある場合は true を返します。

// has_copy.cpp
// compile with: /clr
ref struct R {
   R(R% r) {}
};
int main() {
   System::Console::WriteLine(__has_copy(R));
}

__has_finalizer(type)

(Visual C++ コンポーネント拡張ではサポートされていません)。CLR 型にファイナライザーがある場合は true を返します。 詳細については、「Visual C++ のデストラクター、およびファイナライザー」を参照してください。

// has_finalizer.cpp
// compile with: /clr
using namespace System;
ref struct R {
   ~R() {}
protected:
   !R() {}
};
int main() {
   Console::WriteLine(__has_finalizer(R));
}

__has_nothrow_assign(type)

コピー代入演算子が空の例外の指定がある場合は true を返します。

// has_nothrow_assign.cpp
#include <stdio.h>
struct S { 
   void operator=(S& r) throw() {}
};
int main() {
   __has_nothrow_assign(S) == true ? 
      printf("true\n") : printf("false\n");
}

__has_nothrow_constructor(type)

既定のコンストラクターは、空の例外の指定がある場合は true を返します。

// has_nothrow_constructor.cpp
#include <stdio.h>
struct S { 
   S() throw() {}
};
int main() {
   __has_nothrow_constructor(S) == true ? 
      printf("true\n") : printf("false\n");
}

__has_nothrow_copy(type)

コピー コンストラクターは、空の例外の指定がある場合は true を返します。

// has_nothrow_copy.cpp
#include <stdio.h>
struct S { 
   S(S& r) throw() {}
};
int main() {
   __has_nothrow_copy(S) == true ? 
      printf("true\n") : printf("false\n");
}

__has_trivial_assign(type)

型に自明な場合は true、コンパイラにより生成された代入演算子を返します。

// has_trivial_assign.cpp
#include <stdio.h>
struct S {};
int main() {
   __has_trivial_assign(S) == true ? 
      printf("true\n") : printf("false\n");
}

__has_trivial_constructor(type)

型に自明な場合は true、コンパイラにより生成されたコンストラクターを返します。

// has_trivial_constructor.cpp
#include <stdio.h>
struct S {};
int main() {
   __has_trivial_constructor(S) == true ? 
      printf("true\n") : printf("false\n");
}

__has_trivial_copy(type)

型に自明な場合は true、コンパイラが生成したコピー コンストラクターを返します。

// has_trivial_copy.cpp
#include <stdio.h>
struct S {};
int main() {
   __has_trivial_copy(S) == true ? 
      printf("true\n") : printf("false\n");
}

__has_trivial_destructor(type)

型に自明な場合は true、コンパイラにより生成されたデストラクターを返します。

// has_trivial_destructor.cpp
#include <stdio.h>
struct S {};
int main() {
   __has_trivial_destructor(S) == true ? 
      printf("true\n") : printf("false\n");
}

__has_user_destructor(type)

プラットフォームにまたはネイティブ型はユーザーが宣言したデストラクターがある場合は true を返します。

// has_user_destructor.cpp
// compile with: /clr
using namespace System;
ref class R {
   ~R() {}
};
int main() {
   Console::WriteLine(__has_user_destructor(R));
}

__has_virtual_destructor(type)

型に仮想デストラクターがある場合は true を返します。

__has_virtual_destructor は、プラットフォームの種類で実行され、プラットフォームの種類のユーザー定義のデストラクターが仮想デストラクターです。

// has_virtual_destructor.cpp
#include <stdio.h>
struct S {
   virtual ~S() {}
};
int main() {
   __has_virtual_destructor(S) == true ? 
      printf("true\n") : printf("false\n");
}

__is_abstract(type)

型が抽象型である場合は true を返します。 ネイティブ抽象型の詳細については、「abstract (C++ コンポーネント拡張)」を参照してください。

__is_abstract は、プラットフォームの種類で使用できます。 1 つ以上のメンバーを持つインターフェイスは 1 以上の抽象メンバーを持つ参照型と同様に、抽象型です。 abstract プラットフォームの種類の詳細については、抽象クラス (C++)を参照します

// is_abstract.cpp
#include <stdio.h>
struct S {
   virtual void Test() = 0;
};
int main() {
   __is_abstract(S) == true ? 
      printf("true\n") : printf("false\n");
}

__is_base_of(base, derived)

型が同じ場合、一つ目の型が 2 番目の型の基本クラスの場合、true を返します。

__is_base_of は、プラットフォームの種類で実行されます。 たとえば、最初の型が インターフェイス クラス (C++ コンポーネント拡張) で、2 番目の型がインターフェイスを実装すれば true を返します。

// is_base_of.cpp
#include <stdio.h>
struct S {};
struct T : public S {};
int main() {
   __is_base_of(S, T) == true ? 
      printf("true\n") : printf("false\n");
   __is_base_of(S, S) == true ? 
      printf("true\n") : printf("false\n");
}

__is_class(type)

ネイティブ型がクラスまたは構造体である場合に true を返します。

// is_class.cpp
#include <stdio.h>
struct S {};
int main() {
   __is_class(S) == true ? 
      printf("true\n") : printf("false\n");
}

__is_convertible_to(from, to)

最初の型が 2 番目の型に変換できる場合は、TRUE を返します。

// is_convertible_to.cpp
#include <stdio.h>
struct S {};
struct T : public S {};
int main() {
   S * s = new S;
   T * t = new T;
   s = t;
   __is_convertible_to(T, S) == true ? 
      printf("true\n") : printf("false\n");
}

__is_delegate(type)

type がデリゲートの場合、true を返します。 詳細については、「delegate (C++ コンポーネント拡張)」を参照してください。

// is_delegate.cpp
// compile with: /clr
delegate void MyDel();
int main() {
   System::Console::WriteLine(__is_delegate(MyDel));
}

__is_empty(type)

型にインスタンス データ メンバーがない場合に true を返します。

// is_empty.cpp
#include <stdio.h>
struct S {
   int Test() {}
   static int i;
};
int main() {
   __is_empty(S) == true ? 
      printf("true\n") : printf("false\n");
}

__is_enum(type)

型がネイティブな列挙型 (Enum) である場合に true を返します。

// is_enum.cpp
#include <stdio.h>
enum E { a, b };
struct S {
   enum E2 { c, d };   
};
int main() {
   __is_enum(E) == true ? 
      printf("true\n") : printf("false\n");
   __is_enum(S::E2) == true ? 
      printf("true\n") : printf("false\n");
}

__is_interface_class(type)

プラットフォーム渡られたらインターフェイスに true を返します。 詳細については、「インターフェイス クラス (C++ コンポーネント拡張)」を参照してください。

// is_interface_class.cpp
// compile with: /clr
using namespace System;
interface class I {};
int main() {
   Console::WriteLine(__is_interface_class(I));
}

__is_pod(type)

型がコンストラクターを持たないクラスまたは共用体またはプライベートまたは保護されたな非静的なメンバーは、基本クラスの仮想関数の場合、true を返します。 POD 型の詳細については、" C++ 標準、セクション 8.5.1/1、9/4、3.9/10 を参照してください。

__is_pod は 基本型に FALSE を返します。

// is_pod.cpp
#include <stdio.h>
struct S {};
int main() {
   __is_pod(S) == true ? 
      printf("true\n") : printf("false\n");
}

__is_polymorphic(type)

ネイティブ型に仮想関数がある場合は true を返します。

// is_polymorphic.cpp
#include <stdio.h>
struct S {
   virtual void Test(){}
};
int main() {
   __is_polymorphic(S) == true ? 
      printf("true\n") : printf("false\n");
}

__is_ref_array(type)

渡られたらプラットフォームの配列は true を返します。 詳細については、「配列 (C++ コンポーネント拡張)」を参照してください。

// is_ref_array.cpp
// compile with: /clr
using namespace System;
int main() {
   array<int>^ x = gcnew array<int>(10);
   Console::WriteLine(__is_ref_array(array<int>));
}

__is_ref_class(type)

参照クラスに渡られたら true を返します。 ユーザー定義の参照型の詳細については、「クラスと構造体 (C++ コンポーネント拡張)」を参照してください。

// is_ref_class.cpp
// compile with: /clr
using namespace System;
ref class R {};
int main() {
   Console::WriteLine(__is_ref_class(Buffer));
   Console::WriteLine(__is_ref_class(R));
}

__is_sealed(type)

渡られたらプラットフォーム true を返すか、ネイティブ型はシールされて説明しました。 詳細については、「sealed (C++ コンポーネント拡張)」を参照してください。

// is_sealed.cpp
// compile with: /clr
ref class R sealed{};
int main() {
   System::Console::WriteLine(__is_sealed(R));
}

__is_simple_value_class(type)

ガベージ コレクション ヒープへの参照を含まない値型に渡られたら true を返します。 ユーザー定義の値型の詳細については、「クラスと構造体 (C++ コンポーネント拡張)」を参照してください。

// is_simple_value_class.cpp
// compile with: /clr
using namespace System;
ref class R {};
value struct V {};
value struct V2 {
   R ^ r;   // not a simnple value type
};
int main() {
   Console::WriteLine(__is_simple_value_class(V));
   Console::WriteLine(__is_simple_value_class(V2));
}

__is_union(type)

型が共用体である場合に true を返します。

// is_union.cpp
#include <stdio.h>
union A {
   int i;
   float f;
};
int main() {
   __is_union(A) == true ? 
      printf("true\n") : printf("false\n");
}

__is_value_class(type)

値型に渡られたら true を返します。 ユーザー定義の値型の詳細については、「クラスと構造体 (C++ コンポーネント拡張)」を参照してください。

// is_value_class.cpp
// compile with: /clr
value struct V {};
int main() {
   System::Console::WriteLine(__is_value_class(V));
}

Windows ランタイム

解説

__has_finalizer(type) の型の特徴は、このプラットフォームがファイナライザーをサポートしていないため、サポートされていません。

要件

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

共通言語ランタイム

解説

(この機能のプラットフォーム固有の解説はありません。)

要件

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

次のコード例は /clr コンパイルのコンパイラ型の特徴を公開するときは、クラス テンプレートを使用する方法を示します。 詳細については、「Windows ランタイムおよびマネージ テンプレート (C++ コンポーネント拡張)」を参照してください。

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

template <class T>
ref struct is_class {
   literal bool value = __is_ref_class(T);
};

ref class R {};

int main () {
   if (is_class<R>::value)
      Console::WriteLine("R is a ref class");
   else
      Console::WriteLine("R is not a ref class");
}

出力

  

参照

概念

ランタイム プラットフォームのコンポーネントの拡張機能