template functions, a special case of constant folding?

James Dunne james.jdunne at gmail.com
Thu Mar 23 22:40:46 PST 2006


Derek Parnell wrote:
> On Thu, 23 Mar 2006 16:34:07 -0700, Hasan Aljudy wrote:
> 
> 
>>Given the following expression:
>>#x = 10 * 5 + y;
>>the compiler will compute 10 * 5 at compile-time and convert the 
>>expression into
>>#x = 50 + y;
>>
>>I think that's called constant-folding, am I right?
>>
>>Why can't this be extended such that for the following function:
>># int add( int x, int y ) { return x + y; }
>>
>>if it's called with constant arguments:
>># x = add( 10, 3 );
>>
> 
> 
> You can almost do this with mixins, except that mixins only allow one to
> mixin a complete statement and not just an expression. What would be useful
> is to allow mixins (or some other similar thing) to generate expressions at
> compile time and not just statements. Something like ...
> 
>  
>  template add(b,c)
>  {
>     b + c;
>  }
> 
>  int main()
>  {
>    int q;
>    q = mixin add!(10,3);
>    return q;
>  }
> 
> Which would generate 
> 
>  int main()
>  {
>    int q;
>    q = 10 + 3;
>    return q;
>  }
> 
> Which the compiler would constant-fold as normal.
> 
> But this is starting to look too much like text macros which Walter is very
> concerned not to have.
> 

Why not reuse alias?  Aliased expressions.

-- 
Regards,
James Dunne



More information about the Digitalmars-d-announce mailing list