compiler assertion

Zhenya zheny at list.ru
Sat Sep 29 08:33:07 PDT 2012


On Saturday, 29 September 2012 at 13:03:31 UTC, Philippe Sigaud 
wrote:
> 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.

I just wanted to do something like template,that takes value 
parametres and deduce it's types,so I guess that I can use code 
that you wrote for it with some type checking.



More information about the Digitalmars-d-learn mailing list