Alias

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

Not everything can be directly aliased. An alias cannot be declared of - for example - a literal:

alias a = 4; //Error

With this template any single entity can be aliased:

alias b = Alias!4; //OK
  1. alias Alias(alias a) = a
  2. alias Alias(T) = T
    alias Alias(T) = T

Examples

// Without Alias this would fail if Args[0] were e.g. a value and
// some logic would be needed to detect when to use enum instead.
alias Head(Args...) = Alias!(Args[0]);
alias Tail(Args...) = Args[1 .. $];

alias Blah = AliasSeq!(3, int, "hello");
static assert(Head!Blah == 3);
static assert(is(Head!(Tail!Blah) == int));
static assert((Tail!Blah)[1] == "hello");
{
    alias a = Alias!123;
    static assert(a == 123);
}
{
    enum e = 1;
    alias a = Alias!e;
    static assert(a == 1);
}
{
    alias a = Alias!(3 + 4);
    static assert(a == 7);
}
{
    alias concat = (s0, s1) => s0 ~ s1;
    alias a = Alias!(concat("Hello", " World!"));
    static assert(a == "Hello World!");
}
{
    alias A = Alias!int;
    static assert(is(A == int));
}
{
    alias A = Alias!(AliasSeq!int);
    static assert(!is(typeof(A[0]))); // An Alias is not an AliasSeq.
    static assert(is(A == int));
}
{
    auto i = 6;
    alias a = Alias!i;
    ++a;
    assert(i == 7);
}

See Also

To alias more than one thing at once, use AliasSeq.

Meta