Static argument optimization

Ary Borenszweig ary at esperanto.org.ar
Tue Oct 7 22:13:39 PDT 2008


Janderson escribió:
> Don wrote:
>> Janderson wrote:
>>> Vladimir Panteleev wrote:
>>>> Hello,
>>>>
>>>> Often I encounter cases where I have a function taking some 
>>>> arguments, and I'd like for the compiler to generate separate code 
>>>> for the function when some of the arguments are known at compile 
>>>> time. For example, consider this function:
>>>>
>>>> int pow(int n, int power)
>>>> {
>>>>     return power==0 ? 1 : n*pow(n, power-1);
>>>> }
>>>>
>>>> I am looking for a way to make the function work at runtime, while 
>>>> pow(n, 3) to be inlined as n*n*n, and pow(2, 3) to be precalculated 
>>>> as 8. Is it possible to do this with some template trickery without 
>>>> having to write three versions of the function? I know macros should 
>>>> be able to do this in theory...
>>>>
>>>
>>> Walter has talked about adding the keyword static in the past to 
>>> generate templates ie:
>>>
>>> int pow(static int n, static int power)
>>> {
>>>
>>> }
>>>
>>> I'm not sure if all the params needed to be constant for it to 
>>> generate a template.  I can't see why your proposal wouldn't work 
>>> with this.
>>>
>>> -Joel
>>
>> No, they don't all need to be constant. The classic example is a 
>> regexp, where the regexp itself is a literal, but the other parameters 
>> are all variables.
> 
> That's good to know.  I was thinking that something like with regexpr 
> all parameters would be marked as static and it would pick-and-choose 
> which are actually static/constant based on the call.
> 
> Off-topic:
> 
> The game I'm working on was finally announced today 
> http://www.leagueoflegends.com/
> 
> I'm excited because I can now talk about it :)

I play DOTA every lunch hour since a couple of months with some friends 
at work! I love it! I'll sure try it. :-)



More information about the Digitalmars-d mailing list