Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
September 28, 2012 compiler assertion | ||||
---|---|---|---|---|
| ||||
Hi! Is it normally,that this simple code does'nt compile with this assertion: dmd: template.c:5542: Identifier* TemplateInstance::genIdent(Objects*): Assertion `global.errors' failed. import std.stdio; import std.typetuple; template sum(T:TypeTuple!U,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); } ? |
September 28, 2012 Re: compiler assertion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Zhenya | On Friday, September 28, 2012 22:19:56 Zhenya wrote: > Hi! > Is it normally,that this simple code does'nt compile with this > assertion: > dmd: template.c:5542: Identifier* > TemplateInstance::genIdent(Objects*): Assertion `global.errors' > failed. It's always a bug in the compiler if you see a compiler assertion. What you're seeing is probably this bug: http://d.puremagic.com/issues/show_bug.cgi?id=8421 - Jonathan M Davis |
September 28, 2012 Re: compiler assertion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Friday, 28 September 2012 at 20:27:07 UTC, Jonathan M Davis wrote:
> On Friday, September 28, 2012 22:19:56 Zhenya wrote:
>> Hi!
>> Is it normally,that this simple code does'nt compile with this
>> assertion:
>> dmd: template.c:5542: Identifier*
>> TemplateInstance::genIdent(Objects*): Assertion `global.errors'
>> failed.
>
> It's always a bug in the compiler if you see a compiler assertion. What you're
> seeing is probably this bug:
>
> http://d.puremagic.com/issues/show_bug.cgi?id=8421
>
> - Jonathan M Davis
Thank you,understood.
|
September 29, 2012 Re: compiler assertion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Zhenya | On Fri, Sep 28, 2012 at 10:32 PM, Zhenya <zheny@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.
|
September 29, 2012 Re: compiler assertion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | On Saturday, 29 September 2012 at 13:03:31 UTC, Philippe Sigaud wrote:
> On Fri, Sep 28, 2012 at 10:32 PM, Zhenya <zheny@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.
|
Copyright © 1999-2021 by the D Language Foundation