D Programming Language 2.0

- Comments
- D
- Search
- Home

Last update Tue Oct 16 09:58:58 2007

# std.traits

Templates with which to extract information about types at compile time.

template ReturnType(alias dg)
template ReturnType(dg)
  Get the type of the return value from a function, a pointer to function, or a delegate.
  Example:

       import std.traits;
       int foo();
       ReturnType!(foo) x;   // x is declared as int

template ParameterTypeTuple(alias dg)
template ParameterTypeTuple(dg)
  Get the types of the paramters to a function, a pointer to function, or a delegate as a tuple.
  Example:

       import std.traits;
       int foo(int, long);
       void bar(ParameterTypeTuple!(foo));      // declares void bar(int, long);
       void abc(ParameterTypeTuple!(foo)[1]);   // declares void abc(long);

template FieldTypeTuple(S)
  Get the types of the fields of a struct or class. This consists of the fields that take up memory space, excluding the hidden fields like the virtual function table pointer.
template BaseTypeTuple(A)
  Get a TypeTuple of the base class and base interfaces of this class or interface. BaseTypeTuple!(Object) returns the empty type tuple.
  Example:

       import std.traits, std.typetuple, std.stdio;
       interface I { }
       class A { }
       class B : A, I { }

       void main()
       {
           alias BaseTypeTuple!(B) TL;
           writeln(typeid(TL));	// prints: (A,I)
       }

template BaseClassesTuple(T)
  Get a TypeTuple of all base classes of this class, in decreasing order. Interfaces are not included. BaseClassesTuple!(Object) yields the empty type tuple.
  Example:

       import std.traits, std.typetuple, std.stdio;
       interface I { }
       class A { }
       class B : A, I { }
       class C : B { }

       void main()
       {
           alias BaseClassesTuple!(C) TL;
           writeln(typeid(TL));	// prints: (B,A,Object)
       }

template TransitiveBaseTypeTuple(T)
  Get a TypeTuple of all base classes of T, in decreasing order, followed by T's interfaces. TransitiveBaseTypeTuple!(Object) yields the empty type tuple.
  Example:

       import std.traits, std.typetuple, std.stdio;
       interface I { }
       class A { }
       class B : A, I { }
       class C : B { }

       void main()
       {
           alias TransitiveBaseTypeTuple!(C) TL;
           writeln(typeid(TL));	// prints: (B,A,Object,I)
       }

template ImplicitConversionTargets(T)
  Returns a tuple with all possible target types of an implicit conversion of a value of type T.
  Important note:
  The possible targets are computed more conservatively than the D 2.005 compiler does, eliminating all dangerous conversions. For example, ImplicitConversionTargets!(double) does not include float.
template isIntegral(T)
  Detect whether T is a built-in integral type
template isFloatingPoint(T)
  Detect whether T is a built-in floating point type
template isNumeric(T)
  Detect whether T is a built-in numeric type
template isSomeString(T)
  Detect whether T is one of the built-in string types
template isAssociativeArray(T)
  Detect whether T is an associative array type
template isStaticArray(T : U[],U)
  Detect whether type T is a static array.
template isDynamicArray(T,U = void)
  Detect whether type T is a dynamic array.
template isArray(T)
  Detect whether type T is an array.
template isExpressionTuple(T...)
  Tells whether the tuple T is an expression tuple.
template unsigned(T)
  Returns the corresponding unsigned type for T. T must be a numeric integral type, otherwise a compile-time error occurs.

Public Domain | Page generated by Ddoc.