std.traits
Templates with which to extract information about types at compile time. Authors:Walter Bright, Tomasz Stachowiak (isExpressionTuple), Andrei Alexandrescu
- Get the type of the return value from a function,
a pointer to function, a delegate, a struct
with an opCall, a pointer to a struct with an opCall,
or a class with an opCall.
Example:
import std.traits; int foo(); ReturnType!(foo) x; // x is declared as int
- 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);
- 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.
- Get the primitive types of the fields of a struct or class, in
topological order.
Example:
struct S1 { int a; float b; } struct S2 { char[] a; union { S1 b; S1 * c; } } alias RepresentationTypeTuple!(S2) R; assert(R.length == 4 && is(R[0] == char[]) && is(R[1] == int) && is(R[2] == float) && is(R[3] == S1*));
- Returns true if and only if T's representation includes at
least one of the following:
- a raw pointer U* and U is not invariant;
- an array U[] and U is not invariant;
- a reference to a class type C and C is not invariant.
- 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) }
- 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) }
- Get a TypeTuple of all interfaces directly or
indirectly inherited by this class or interface. Interfaces do not
repeat if multiply implemented. InterfacesTuple!(Object)
yields the empty type tuple.
Example:
import std.traits, std.typetuple, std.stdio; interface I1 { } interface I2 { } class A : I1, I2 { } class B : A, I1 { } class C : B { } void main() { alias InterfacesTuple!(C) TL; writeln(typeid(TL)); // prints: (I1, I2) }
- 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) }
- Get the type that all types can be implicitly converted to. Useful
e.g. in figuring out an array type from a bunch of initializing
values. Returns void if passed an empty list, or if the
types have no common type.
Example:
alias CommonType!(int, long, short) X; assert(is(X == long)); alias CommonType!(int, char[], short) Y; assert(is(Y == void));
- 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.
- Detect whether T is a built-in integral type. Types bool, char, wchar, and dchar are not considered integral.
- Detect whether T is a built-in floating point type.
- Detect whether T is a built-in numeric type (integral or floating point).
- Detect whether T is one of the built-in string types
- Detect whether T is an associative array type
- Detect whether type T is a static array.
- Detect whether type T is a dynamic array.
- Detect whether type T is an array.
- Tells whether the tuple T is an expression tuple.
- Returns the corresponding unsigned type for T. T must be a numeric integral type, otherwise a compile-time error occurs.
- Returns the most negative value of the numeric type T.
- Removes all qualifiers, if any, from type T.
Example:
static assert(is(Unqual!(int) == int)); static assert(is(Unqual!(const int) == int)); static assert(is(Unqual!(immutable int) == int));
- Evaluates to TypeTuple!(F[T[0]], F[T[1]], ..., F[T[$ - 1]]).
Example:
alias staticMap!(Unqual, int, const int, immutable int) T; static assert(is(T == TypeTuple!(int, int, int)));
- Evaluates to F[T[0]] && F[T[1]] && ... && F[T[$ - 1]].
Example:
static assert(!allSatisfy!(isIntegral, int, double)); static assert(allSatisfy!(isIntegral, int, long));
- Aliases itself to T if the boolean condition is true
and to F otherwise.
Example:
alias Select!(size_t.sizeof == 4, int, long) Int;
- If cond is true, returns a without evaluating b. Otherwise, returns b without evaluating a.