Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


typeid is used to get the Type for a type at compile time.



The name of a type (abstract declarator) for which you want the System::Type object.

typeid is similar to getting the System::Type for a type at run time using GetType or GetType. However, typeid only accepts a type name as a parameter. If you want to use an instance of a type to get its System::Type name, use GetType.

typeid must be able to evaluate a type name (type) at compile time, whereas GetType evaluates the type to return at run time.

typeid can take a native type name or common language runtime alias for the native type name; see .NET Framework Equivalents to C++ Native Types (C++/CLI) for more information.

typeid also works with native types, although it will still return a System::Type. To get a type_info structure, use typeid Operator.

typeid is the sucessor to __typeof in the previous /clr syntax.

// keyword__typeid.cpp
// compile with: /clr
using namespace System;

ref struct G {
   int i;

int main() {
   G ^ pG = gcnew G;
   Type ^ pType = pG->GetType();
   Type ^ pType2 = G::typeid;

   if (pType == pType2)
      Console::WriteLine("typeid and GetType returned the same System::Type");

   typedef float* FloatPtr;
typeid and GetType returned the same System::Type G System.Single*

The following sample shows that a variable of type System::Type can be used to get the attributes on a type. It also shows that for some types, you will have to create a typedef to use typeid.

// keyword__typeid_2.cpp
// compile with: /clr
using namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;

typedef int ^ handle_to_int;
typedef int * pointer_to_int;

public ref class MyClass {};

class MyClass2 {};

ref class AtClass {
   AtClass(Type ^) {
      Console::WriteLine("in AtClass Type ^ constructor");

ref class AtClass2 {
   AtClass2() {
      Console::WriteLine("in AtClass2 constructor");

// Apply the AtClass and AtClass2 attributes to class B
[AtClass(MyClass::typeid), AtClass2]   
ref class B : Attribute {};

int main() {
   Type ^ MyType = B::typeid;

   array<Object^>^ MyArray = MyType -> GetCustomAttributes(true);
   for (int i = 0 ; i < MyArray->Length ; i++ )

   if (int::typeid != pointer_to_int::typeid)
      Console::WriteLine("int::typeid != pointer_to_int::typeid, as expected");

   if (int::typeid == handle_to_int::typeid)
      Console::WriteLine("int::typeid == handle_to_int::typeid, as expected");
True in AtClass2 constructor in AtClass Type ^ constructor AtClass2 System.AttributeUsageAttribute AtClass int::typeid != pointer_to_int::typeid, as expected int::typeid == handle_to_int::typeid, as expected

Compiler option: /clr

© 2015 Microsoft