型別特性的編譯器支援 (C++/CLI 和 C++/CX)
Microsoft C++ 編譯器支援適用於 C++/CLI 和 C++/CX 擴充功能的「型別特性」,可指出型別在編譯時間的各種特性。
所有執行階段
備註
類型特性特別適用於撰寫程式庫的程式設計人員。
下表列出編譯器所支援的型別特性。 如果不符合類型功能名稱所指定的條件,則所有類型特性都會傳回 false
。
(在下列清單中,程式代碼範例只會以 C++/CLI 撰寫。但除非另有說明,否則C++/CX也支持對應的類型特性。「平台類型」一詞是指 Windows 執行階段 類型或 Common Language Runtime 類型。
__has_assign(
類型)
如果平臺或原生類型具有複製指派運算元,則傳
true
回 。ref struct R { void operator=(R% r) {} }; int main() { System::Console::WriteLine(__has_assign(R)); }
__has_copy(
類型)
如果平臺或原生類型具有複製建構函式,則傳
true
回 。ref struct R { R(R% r) {} }; int main() { System::Console::WriteLine(__has_copy(R)); }
__has_finalizer(
類型)
(C++/CX 中不支援)如果 CLR 類型具有完成項目,則傳
true
回 。 如需詳細資訊,請參閱 如何:定義和使用類別和結構(C++/CLI) 中的解構函式和完成項。using namespace System; ref struct R { ~R() {} protected: !R() {} }; int main() { Console::WriteLine(__has_finalizer(R)); }
__has_nothrow_assign(
類型)
如果複製指派運算子有空的例外狀況規格,則傳
true
回 。#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(
類型)
如果預設建構函式有空的例外狀況規格,則傳
true
回 。#include <stdio.h> struct S { S() throw() {} }; int main() { __has_nothrow_constructor(S) == true ? printf("true\n") : printf("false\n"); }
__has_nothrow_copy(
類型)
如果複製建構函式有空的例外狀況規格,則傳
true
回 。#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(
類型)
如果類型具有簡單且編譯程式產生的指派運算子,則傳
true
回 。#include <stdio.h> struct S {}; int main() { __has_trivial_assign(S) == true ? printf("true\n") : printf("false\n"); }
__has_trivial_constructor(
類型)
如果型別具有簡單的編譯程式產生的建構函式,則傳
true
回 。#include <stdio.h> struct S {}; int main() { __has_trivial_constructor(S) == true ? printf("true\n") : printf("false\n"); }
__has_trivial_copy(
類型)
如果類型具有簡單、編譯程式產生的複製建構函式,則傳
true
回 。#include <stdio.h> struct S {}; int main() { __has_trivial_copy(S) == true ? printf("true\n") : printf("false\n"); }
__has_trivial_destructor(
類型)
如果類型具有簡單、編譯程式產生的解構函式,則傳
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(
類型)
如果平臺或原生類型具有使用者宣告的解構函式,則傳
true
回 。// has_user_destructor.cpp using namespace System; ref class R { ~R() {} }; int main() { Console::WriteLine(__has_user_destructor(R)); }
__has_virtual_destructor(
類型)
如果類型具有虛擬解構函式,則傳
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(
類型)
如果類型是抽象類型,則傳
true
回 。 如需原生抽象型別的詳細資訊,請參閱抽象類別。__is_abstract
也適用於平台類型。 至少有一個成員的介面是參考類型,至少有一個抽象成員的介面是參考類型。 如需抽象平台型別的詳細資訊,請參閱 abstract。// 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
)
如果第一個類型是第二個類型的基類,或這兩個類型都相同,則傳
true
回 。__is_base_of
也適用於平台類型。 例如,如果第一個類型是介面類別,而第二個類型會實作 介面,則會傳回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(
類型)
如果類型是原生類別或結構,則傳
true
回 。#include <stdio.h> struct S {}; int main() { __is_class(S) == true ? printf("true\n") : printf("false\n"); }
__is_convertible_to(
from
,
to
)
如果第一個類型可以轉換成第二個類型,則傳
true
回 。#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
是委派,則傳true
回 。 如需詳細資訊,請參閱 delegate (C++/CLI 和 C++/CX)。delegate void MyDel(); int main() { System::Console::WriteLine(__is_delegate(MyDel)); }
__is_empty(
類型)
如果類型沒有實體資料成員,則傳
true
回 。#include <stdio.h> struct S { int Test() {} static int i; }; int main() { __is_empty(S) == true ? printf("true\n") : printf("false\n"); }
__is_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(
類型)
如果傳遞平台介面,則傳
true
回 。 如需詳細資訊,請參閱介面類別。// is_interface_class.cpp using namespace System; interface class I {}; int main() { Console::WriteLine(__is_interface_class(I)); }
__is_pod(
類型)
如果類型是沒有建構函式或私用或受保護的非靜態成員、沒有基類,也沒有虛擬函式的類別或等位,則傳
true
回 。 如需 POD 的詳細資訊,請參閱 C++ 標準的 8.5.1/1、9/4 和 3.9/10 小節。__is_pod
對於基本類型會傳回 false。#include <stdio.h> struct S {}; int main() { __is_pod(S) == true ? printf("true\n") : printf("false\n"); }
__is_polymorphic(
類型)
如果原生類型具有虛擬函式,則傳
true
回 。#include <stdio.h> struct S { virtual void Test(){} }; int main() { __is_polymorphic(S) == true ? printf("true\n") : printf("false\n"); }
__is_ref_array(
類型)
如果傳遞平台陣列,則傳
true
回 。 如需詳細資訊,請參閱陣列。using namespace System; int main() { array<int>^ x = gcnew array<int>(10); Console::WriteLine(__is_ref_array(array<int>)); }
__is_ref_class(
類型)
如果傳遞參考類別,則傳
true
回 。 如需使用者定義的參考型別的詳細資訊,請參閱類別和結構。using namespace System; ref class R {}; int main() { Console::WriteLine(__is_ref_class(Buffer)); Console::WriteLine(__is_ref_class(R)); }
__is_sealed(
類型)
如果傳遞標示為密封的平臺或原生類型,則傳
true
回 。 如需詳細資訊,請參閱 sealed。ref class R sealed{}; int main() { System::Console::WriteLine(__is_sealed(R)); }
__is_simple_value_class(
類型)
true
如果傳遞的實值型別不包含垃圾收集堆積的參考,則傳回 。 如需使用者定義的實值型別的詳細資訊,請參閱類別和結構。using namespace System; ref class R {}; value struct V {}; value struct V2 { R ^ r; // not a simple value type }; int main() { Console::WriteLine(__is_simple_value_class(V)); Console::WriteLine(__is_simple_value_class(V2)); }
__is_union(
類型)
如果類型為等位,則傳
true
回 。#include <stdio.h> union A { int i; float f; }; int main() { __is_union(A) == true ? printf("true\n") : printf("false\n"); }
__is_value_class(
類型)
如果傳遞實值型別,則傳
true
回 。 如需使用者定義的實值型別的詳細資訊,請參閱類別和結構。value struct V {}; int main() { System::Console::WriteLine(__is_value_class(V)); }
Windows 執行階段
備註
不支援 __has_finalizer(
type)
型別特性,因為此平台不支援完成項。
需求
編譯器選項:/ZW
通用語言執行平台
備註
(沒有這項功能的平台特定備註。)
需求
編譯器選項:/clr
範例
範例
下列程式碼範例說明如何使用類別範本來公開 /clr
編譯的編譯器類型特性。 如需詳細資訊,請參閱 Windows 執行階段與受控範本。
// 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");
}
R is a ref class