www.digitalmars.com         C & C++   DMDScript  

D - constant folding

reply "Sean L. Palmer" <palmer.sean verizon.net> writes:
I propose to enable constant folding and inlining where it is not currently
allowed:
(it should at least *try* to evaluate it at compile time, since the
parameters are all constants)

int bar(int t) { return t*2; }

template templ(int t)

{

    struct foo

    {

        enum { result = bar(t) } // error: Integer constant expression
expected instead of bar(5)

        enum { result = t*2 } // works

    }

}

int main()

{

    int k = instance templ(5).foo.result;

    printf("%d",k);

    return 0; // it'd be really nice if this printed 10!

}



Performing Pre-Build Event...

c:\DMD\src\test>..\..\bin\dmd test.d -debug -g -unittest

test.d(266): Integer constant expression expected instead of bar(5)

test : error PRJ0002 : error result returned from 'c:\Temp\BAT000003.bat'.
May 09 2003
parent "Walter" <walter digitalmars.com> writes:
The problem here is the order in which things are done internally. It's not
so easy to change!

"Sean L. Palmer" <palmer.sean verizon.net> wrote in message
news:b9i2bu$1ufb$1 digitaldaemon.com...
 I propose to enable constant folding and inlining where it is not
currently
 allowed:
 (it should at least *try* to evaluate it at compile time, since the
 parameters are all constants)

 int bar(int t) { return t*2; }

 template templ(int t)

 {

     struct foo

     {

         enum { result = bar(t) } // error: Integer constant expression
 expected instead of bar(5)

         enum { result = t*2 } // works

     }

 }

 int main()

 {

     int k = instance templ(5).foo.result;

     printf("%d",k);

     return 0; // it'd be really nice if this printed 10!

 }



 Performing Pre-Build Event...

 c:\DMD\src\test>..\..\bin\dmd test.d -debug -g -unittest

 test.d(266): Integer constant expression expected instead of bar(5)

 test : error PRJ0002 : error result returned from 'c:\Temp\BAT000003.bat'.
May 10 2003