编译器支持类型特征(C++ 组件扩展)

编译器支持 键入字符,以及指示类型的各种特性在编译过程。

所有运行时

备注

类型字符写入库的程序员特别有用。

下表说明了编译器所支持的类型可访问性。 如果条件指定名称类型字符不满足,所有字符类型返回 false。

(在表的列的描述中,代码示例C++/CLI仅编写。 但对应的类型字符为还支持Visual C++ 组件扩展 除非另外声明。 术语,“平台类型”是指 Windows 运行时 类型或公共语言运行时 (CLR) 类型。)

Type Trait

说明

__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 对平台类型也有效。 至少具有一个成员的接口是抽象类型,与至少具有一个抽象成员的引用类型。 有关 64 位平台支持的更多信息,请参见 抽象类 (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)

返回 true,如果第一个类型是第二个类型的基类,在中,如果两个类型相同。

__is_base_of 还使用平台类型。 例如,它将返回 true,如果 第一个类型 是 接口类(C++ 组件扩展),且 type2 实现接口。

// 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)

如果第一种类型可转换为另一个类型,则返回 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。 有关详细信息,请参阅委托(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)

如果类型为本机枚举,则返回 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。 有关详细信息,请参阅 C++ 标准中的 8.5.1/1, 9/4 和 3.9/10 节。

__is_pod 将返回错误在基础类型上。

// 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,则传入平台或本机类型指示了封装。 有关详细信息,请参阅已密封(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");
}

Output

  

请参见

概念

适用于运行时平台的组件扩展