compiler assertion

Philippe Sigaud philippe.sigaud at gmail.com
Sat Sep 29 06:03:51 PDT 2012


On Fri, Sep 28, 2012 at 10:32 PM, Zhenya <zheny at list.ru> wrote:

> Thank you,understood.

This should work, hopefully:

import std.stdio;
import std.typetuple;

template sum(U...)
{
        static if(U.length == 0)
                enum sum = 0;
        else
                enum sum = U[0]+sum!(U[1..$]);
}

void main()
{
        enum s = sum!(1,2,3,4);
        writeln(s);
}

You can also do it with a standard function, and forcing its execution
at compile-time by using 'enum':


U[0] sum(U...)(U u) if (U.length > 0)
{
    U[0] result =0;
    foreach(value;u)
        result += value;
    return result;
}

void main()
{
        enum s = sum(1,2,3,4);
        writeln(s);
}

Note that doing that without any check on the U's is a bit dangerous.


More information about the Digitalmars-d-learn mailing list