다음을 통해 공유


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_assign.cpp
// compile with: /clr
ref struct R {
   void operator=(R% r) {}
};
int main() {
   System::Console::WriteLine(__has_assign(R));
}

__has_copy(type)

Returns true if the CLR or native type has a copy constructor.

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

__has_finalizer(type)

Returns true if the CLR type has a finalizer. See Destructors and Finalizers in Visual C++ for more information.

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

Returns true if a copy assignment operator has an empty exception specification.

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

Returns true if the default constructor has an empty exception specification.

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

Returns true if the copy constructor has an empty exception specification.

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

Returns true if the type has a trivial, compiler-generated assignment operator.

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

Returns true if the type has a trivial, compiler-generated constructor.

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

Returns true if the type has a trivial, compiler-generated copy constructor.

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

Returns true if the type has a trivial, compiler-generated destructor.

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

Returns true if the CLR or native type has a user-declared destructor.

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

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.

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

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

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

Returns true if the type is a native class or struct.

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

Returns true if the first type can be converted to the second type.

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

Returns true if type is a delegate. For more information, see delegate.

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

__is_empty(type)

Returns true if the type has no instance data members.

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

Returns true if the type is a native enum.

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

Returns true if passed a CLR interface. For more information, see interface class.

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

__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_pod.cpp
#include <stdio.h>
struct S {};
int main() {
   __is_pod(S) == true ? 
      printf("true\n") : printf("false\n");
}

__is_polymorphic(type)

Returns true if a native type has virtual functions.

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

Returns true if passed a CLR array. For more information, see array (Visual 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)

Returns true if passed a reference class. For more information on user-defined reference types, see Classes and Structs (Managed).

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

Returns true if passed a CLR or native type marked sealed. For more information, see sealed.

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

__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_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)

Returns true if a type is a union.

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

Returns true if passed a value type. For more information on user-defined value types, see Classes and Structs (Managed).

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

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

See Also

Concepts

Language Features for Targeting the CLR