Compiler Support for Type Traits (C++ Component Extensions)
The compiler supports type traits, which indicate various characteristics of a type at compile time.
All Runtimes
Remarks
Type traits are especially useful to programmers who write libraries.
The following table lists the type traits that are supported by the compiler. All type traits return false if the condition specified by the name of the type trait is not met.
(In the Description column of the table, code examples are written only in C++/CLI. But the corresponding type trait is also supported in Visual C++ component extensions unless stated otherwise. The term, "platform type" refers to either Windows Runtime types or common language runtime types.)
Type Trait |
Description |
---|---|
__has_assign(type) |
Returns true if the platform or native type has a copy assignment operator.
|
__has_copy(type) |
Returns true if the platform or native type has a copy constructor.
|
__has_finalizer(type) |
(Not supported in Visual C++ component extensions.) Returns true if the CLR type has a finalizer. See Destructors and Finalizers in Visual C++ for more information.
|
__has_nothrow_assign(type) |
Returns true if a copy assignment operator has an empty exception specification.
|
__has_nothrow_constructor(type) |
Returns true if the default constructor has an empty exception specification.
|
__has_nothrow_copy(type) |
Returns true if the copy constructor has an empty exception specification.
|
__has_trivial_assign(type) |
Returns true if the type has a trivial, compiler-generated assignment operator.
|
__has_trivial_constructor(type) |
Returns true if the type has a trivial, compiler-generated constructor.
|
__has_trivial_copy(type) |
Returns true if the type has a trivial, compiler-generated copy constructor.
|
__has_trivial_destructor(type) |
Returns true if the type has a trivial, compiler-generated destructor.
|
__has_user_destructor(type) |
Returns true if the platform or native type has a user-declared destructor.
|
__has_virtual_destructor(type) |
Returns true if the type has a virtual destructor. __has_virtual_destructor also works on platform types, and any user-defined destructor in a platform type is a virtual destructor.
|
__is_abstract(type) |
Returns true if the type is an abstract type. For more information on native abstract types, see abstract (C++ Component Extensions). __is_abstract also works for platform types. An interface with at least one member is an abstract type, as is a reference type with at least one abstract member. For more information on abstract platform types, see Abstract Classes (C++)
|
__is_base_of(base,derived) |
Returns true if the first type is a base class of the second type, of if both types are the same. __is_base_of also works on platform types. For example, it will return true if the first type is an interface class (C++ Component Extensions) and the second type implements the interface.
|
__is_class(type) |
Returns true if the type is a native class or struct.
|
__is_convertible_to(from, to) |
Returns true if the first type can be converted to the second type.
|
__is_delegate(type) |
Returns true if type is a delegate. For more information, see delegate (C++ Component Extensions).
|
__is_empty(type) |
Returns true if the type has no instance data members.
|
__is_enum(type) |
Returns true if the type is a native enum.
|
__is_interface_class(type) |
Returns true if passed a platform interface. For more information, see interface class (C++ Component Extensions).
|
__is_pod(type) |
Returns true if the type is a class or union with no constructor or private or protected non-static members, no base classes, and no virtual functions. See the C++ standard, sections 8.5.1/1, 9/4, and 3.9/10 for more information on PODs. __is_pod will return false on fundamental types.
|
__is_polymorphic(type) |
Returns true if a native type has virtual functions.
|
__is_ref_array(type) |
Returns true if passed a platform array. For more information, see Arrays (C++ Component Extensions).
|
__is_ref_class(type) |
Returns true if passed a reference class. For more information on user-defined reference types, see Classes and Structs (C++ Component Extensions).
|
__is_sealed(type) |
Returns true if passed a platform or native type marked sealed. For more information, see sealed (C++ Component Extensions).
|
__is_simple_value_class(type) |
Returns true if passed a value type that contains no references to the garbage-collected heap. For more information on user-defined value types, see Classes and Structs (C++ Component Extensions).
|
__is_union(type) |
Returns true if a type is a union.
|
__is_value_class(type) |
Returns true if passed a value type. For more information on user-defined value types, see Classes and Structs (C++ Component Extensions).
|
Windows Runtime
Remarks
The __has_finalizer(type) type trait is not supported because this platform does not support finalizers.
Requirements
Compiler option: /ZW
Common Language Runtime
Remarks
(There are no platform-specific remarks for this feature.)
Requirements
Compiler option: /clr
Examples
Example
The following code example shows how to use a class template to expose a compiler type trait for a /clr compilation. For more information, see Windows Runtime and Managed Templates (C++ Component Extensions).
// 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
R is a ref class