Compiler Support for Type Traits
The compiler's support for type traits allows library writers to determine various characteristics of a type at compile time.
All type traits return false if the specified conditions are not met.
These traits are necessary but not sufficient to implement some of the tr1 type traits like std::tr1::is_pod. The compiler intrinsics provide type information that is otherwise unavailable.
Remarks
The compiler supports the following type traits:
Type Trait |
Description |
---|---|
__has_assign(type) |
Returns true if the CLR or native type has a copy assignment operator.
|
__has_copy(type) |
Returns true if the CLR or native type has a copy constructor.
|
__has_finalizer(type) |
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 CLR 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 CLR types, and any user-defined destructor in a CLR 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 (Visual C+). __is_abstract also works for CLR 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 CLR 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 CLR types. For example, it will return true if the first type is an interface class 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.
|
__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 CLR interface. For more information, see interface class.
|
__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 CLR array. For more information, see array (Visual C+).
|
__is_ref_class(type) |
Returns true if passed a reference class. For more information on user-defined reference types, see Classes and Structs (Managed).
|
__is_sealed(type) |
Returns true if passed a CLR or native type marked sealed. For more information, see sealed.
|
__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 (Managed).
|
__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 (Managed).
|
Example
This sample shows how to use a class template to expose a compiler type trait for a /clr compilation. For more information, see Managed Templates,
// 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
Requirements
Compiler option: /clr