Templates with which to manipulate type tuples (also known as type lists).
Some operations on type tuples are built in to the language,
such as TL[n] which gets the nth type from the
type tuple. TL[lwr .. upr] returns a new type
list that is a slice of the old one.
Several templates in this module use or operate on eponymous templates that
take a single argument and evaluate to a boolean constant. Such templates
are referred to as template predicates.
Creates a typetuple out of a sequence of zero or more types.
import std.typetuple;
alias TL = TypeTuple!(int, double);
int foo(TL td) // same as int foo(int, double);
{
return td[0] + cast(int)td[1];
}
alias TL = TypeTuple!(int, double);
alias Types = TypeTuple!(TL, char);
static assert(is(Types == TypeTuple!(int, double, char)));
Returns the index of the first occurrence of type T in the sequence of zero or more types TList. If not found, -1 is returned.
import std.typetuple;
import std.stdio;
void foo()
{
writefln("The index of long is %s",
staticIndexOf!(long, TypeTuple!(int, long, double)));
// prints: The index of long is 1
}
Kept for backwards compatibility
Returns a typetuple created from TList with the first occurrence, if any, of T removed.
alias Types = TypeTuple!(int, long, double, char);
alias TL = Erase!(long, Types);
static assert(is(TL == TypeTuple!(int, double, char)));
Returns a typetuple created from TList with the all occurrences, if any, of T removed.
alias Types = TypeTuple!(int, long, long, int);
alias TL = EraseAll!(long, Types);
static assert(is(TL == TypeTuple!(int, int)));
Returns a typetuple created from TList with the all duplicate types removed.
alias Types = TypeTuple!(int, long, long, int, float);
alias TL = NoDuplicates!(Types);
static assert(is(TL == TypeTuple!(int, long, float)));
Returns a typetuple created from TList with the first occurrence of type T, if found, replaced with type U.
alias Types = TypeTuple!(int, long, long, int, float);
alias TL = Replace!(long, char, Types);
static assert(is(TL == TypeTuple!(int, char, long, int, float)));
Returns a typetuple created from TList with all occurrences of type T, if found, replaced with type U.
alias Types = TypeTuple!(int, long, long, int, float);
alias TL = ReplaceAll!(long, char, Types);
static assert(is(TL == TypeTuple!(int, char, char, int, float)));
Returns a typetuple created from TList with the order reversed.
alias Types = TypeTuple!(int, long, long, int, float);
alias TL = Reverse!(Types);
static assert(is(TL == TypeTuple!(float, int, long, long, int)));
Returns the type from TList that is the most derived from type T. If none are found, T is returned.
class A { }
class B : A { }
class C : B { }
alias Types = TypeTuple!(A, C, B);
MostDerived!(Object, Types) x; // x is declared as type C
static assert(is(typeof(x) == C));
Returns the typetuple TList with the types sorted so that the most derived types come first.
class A { }
class B : A { }
class C : B { }
alias Types = TypeTuple!(A, C, B);
alias TL = DerivedToFront!(Types);
static assert(is(TL == TypeTuple!(C, B, A)));
Evaluates to TypeTuple!(F!(T[0]), F!(T[1]), ..., F!(T[$ - 1])).
alias TL = staticMap!(Unqual, int, const int, immutable int);
static assert(is(TL == TypeTuple!(int, int, int)));
Tests whether all given items satisfy a template predicate, i.e. evaluates to F!(T[0]) && F!(T[1]) && ... && F!(T[$ - 1]).
Evaluation is not short-circuited if a false result is encountered; the template predicate must be instantiable with all the given items.
static assert(!allSatisfy!(isIntegral, int, double));
static assert( allSatisfy!(isIntegral, int, long));
Tests whether all given items satisfy a template predicate, i.e. evaluates to F!(T[0]) || F!(T[1]) || ... || F!(T[$ - 1]).
Evaluation is not short-circuited if a true result is encountered; the template predicate must be instantiable with all the given items.
static assert(!anySatisfy!(isIntegral, string, double));
static assert( anySatisfy!(isIntegral, int, double));
Filters a TypeTuple using a template predicate. Returns a TypeTuple of the elements which satisfy the predicate.
alias Types1 = TypeTuple!(string, wstring, dchar[], char[], dstring, int);
alias TL1 = Filter!(isNarrowString, Types1);
static assert(is(TL1 == TypeTuple!(string, wstring, char[])));
alias Types2 = TypeTuple!(int, byte, ubyte, dstring, dchar, uint, ulong);
alias TL2 = Filter!(isUnsigned, Types2);
static assert(is(TL2 == TypeTuple!(ubyte, uint, ulong)));
Negates the passed template predicate.
import std.traits;
alias isNoPointer = templateNot!isPointer;
static assert(!isNoPointer!(int*));
static assert(allSatisfy!(isNoPointer, string, char, float));
Combines several template predicates using logical AND, i.e. constructs a new predicate which evaluates to true for a given input T if and only if all of the passed predicates are true for T.
The predicates are evaluated from left to right, aborting evaluation in a short-cut manner if a false result is encountered, in which case the latter instantiations do not need to compile.
alias storesNegativeNumbers = templateAnd!(isNumeric, templateNot!isUnsigned);
static assert(storesNegativeNumbers!int);
static assert(!storesNegativeNumbers!string && !storesNegativeNumbers!uint);
// An empty list of predicates always yields true.
alias alwaysTrue = templateAnd!();
static assert(alwaysTrue!int);
Combines several template predicates using logical OR, i.e. constructs a new predicate which evaluates to true for a given input T if and only at least one of the passed predicates is true for T.
The predicates are evaluated from left to right, aborting evaluation in a short-cut manner if a true result is encountered, in which case the latter instantiations do not need to compile.
alias isPtrOrUnsigned = templateOr!(isPointer, isUnsigned);
static assert( isPtrOrUnsigned!uint && isPtrOrUnsigned!(short*));
static assert(!isPtrOrUnsigned!int && !isPtrOrUnsigned!(string));
// An empty list of predicates never yields true.
alias alwaysFalse = templateOr!();
static assert(!alwaysFalse!int);