phobos.sys.meta

Templates to manipulate template parameter sequences (also known as alias sequences).

Some operations on alias sequences are built into the language, such as S[i], which accesses the element at index i in the sequence. S[low .. high] returns a new alias sequence that is a slice of the old one.

For more information, see Compile-time Sequences.

One thing that should be noted is that while the templates provided in this module can be extremely useful, they generally should not be used with lists of values. The language uses alias sequences for a variety of things (including both parameter lists and argument lists), so they can contain types, symbols, values, or a mixture of them all. The ability to manipulate types and symbols within alias sequences is vital, because that's really the only way to do it. However, because D has CTFE (Compile-Time Function Evaluation), making it possible to call many functions at compile time, if code needs to be able to manipulate values at compile-time, CTFE is typically much more efficient and easier to do. Instantiating a bunch of templates to manipulate values is incredibly inefficient in comparison.

So, while many of the templates in this module will work with values simply because alias sequences can contain values, most code should restrict itself to using them for operating on types or symbols - i.e. the stuff where CTFE can't be used. That being said, there will be times when one can be used to feed into the other. E.G.

alias Types = AliasSeq!(int, byte, ulong, int[10]);

enum Sizeof(T) = T.sizeof;

alias sizesAsAliasSeq = Map!(Sizeof, Types);
static assert(sizesAsAliasSeq == AliasSeq!(4, 1, 8, 40));

enum size_t[] sizes = [sizesAsAliasSeq];
static assert(sizes == [4, 1, 8, 40]);

Just be aware that if CTFE can be used for a particular task, it's better to use CTFE than to manipulate alias sequences with the kind of templates provided by this module.

$(DIVC quickindex,

CategoryTemplates
Building blocksAlias AliasSeq
Alias sequence filteringFilter Stride Unique
Alias sequence transformationMap Reverse
Alias sequence searchingall any indexOf
Template predicatesAnd Not Or
Template instantiationApplyLeft ApplyRight Instantiate

References: Based on ideas in Table 3.1 from Modern C++ Design, Andrei Alexandrescu (Addison-Wesley Professional, 2001)

Members

Aliases

Alias
alias Alias(alias a) = a
alias Alias(T) = T

Allows aliasing of any single symbol, type or compile-time expression.

AliasSeq
alias AliasSeq(TList...) = TList

Creates a sequence of zero or more aliases. This is most commonly used as template parameters or arguments.

Instantiate
alias Instantiate(alias Template, Args...) = Template!Args

Instantiates the given template with the given arguments and evaluates to the result of that template.

Templates

And
template And(Preds...)

Combines multiple template predicates into a single template predicate using logical AND - i.e. for the resulting predicate to be true with a particular argument, all of the predicates must be true with that argument.

ApplyLeft
template ApplyLeft(alias Template, Args...)

ApplyLeft does a partial application of its arguments, providing a way to bind a set of arguments to the given template while delaying actually instantiating that template until the full set of arguments is provided. The "Left" in the name indicates that the initial arguments are one the left-hand side of the argument list when the given template is instantiated.

ApplyRight
template ApplyRight(alias Template, Args...)

ApplyRight does a partial application of its arguments, providing a way to bind a set of arguments to the given template while delaying actually instantiating that template until the full set of arguments is provided. The "Right" in the name indicates that the initial arguments are one the right-hand side of the argument list when the given template is instantiated.

Filter
template Filter(alias Pred, Args...)

Filters an AliasSeq using the given template predicate.

Map
template Map(alias Fun, Args...)

Map takes a template and applies it to every element in the given AliasSeq, resulting in an AliasSeq with the transformed elements.

Not
template Not(alias Pred)

Evaluates to a template predicate which negates the given predicate.

Or
template Or(Preds...)

Combines multiple template predicates into a single template predicate using logical OR - i.e. for the resulting predicate to be true with a particular argument, at least one of the predicates must be true with that argument.

Reverse
template Reverse(Args...)

Takes an AliasSeq and result in an AliasSeq where the order of the elements has been reversed.

Stride
template Stride(int stepSize, Args...)

Evaluates to an AliasSeq which only contains every nth element from the AliasSeq that was passed in, where n is stepSize.

Unique
template Unique(alias Cmp, Args...)

Evaluates to an AliasSeq which contains no duplicate elements.

all
template all(alias Pred, Args...)

Whether the given template predicate is true for all of the elements in the given AliasSeq.

any
template any(alias Pred, Args...)

Whether the given template predicate is true for any of the elements in the given AliasSeq.

indexOf
template indexOf(alias Pred, Args...)

Evaluates to the index of the first element where Pred!(Args[i]) is true.

Meta