static parameters

Don Clugston dac at nospam.com.au
Thu Aug 30 23:28:08 PDT 2007


Robert Fraser wrote:
> Bill Baxter Wrote:
> 
>> On page 30 of WalterAndrei.pdf it talks about static function parameters.
>>
>> It says
>>     void foo(static int i, int j) { ... }
>> will become a synonym for the template
>>     void foo(int i)(int j) { ... }
>>
>> And the reason given is it's supposed to make templates "easier for 
>> mortals".
>>
>> I don't really see how that makes anything easier.  It's longer to type 
>> first of all.  And you still have to be aware of the compile-time / 
>> runtime distinction, this just makes it a little harder to see.  And it 
>> introduces issues like what to do if there are both template parameters 
>> and static arguments, and what to do with __traits for such functions, 
>> and what the parameters tuple should look like, and whether you can have 
>> foo(int i, static j), and whether you can still call it as foo!(7)(5), 
>> and if you can call foo(int i)(int j) as foo(3,x)?
>>
>> And if it is just like a template can you take &foo?  If not, how do you 
>> take the address of a particular specialization of the function?  Do you 
>> revert back to foo!(3) then?
>>
>> In short it seems like a classic case of a feature that adds little 
>> benefit, but a fair number of new rules that everyone will have to 
>> remember, and in the end it could very likely stomp on the toes of some 
>> other future features that would be much more useful.
>>
>> As far as I can tell, this is just another way to do something we can 
>> already do, and all we gain from it is the ability to save two 
>> characters on the calling side for functions that are only templated on 
>> const values.
>>
>> --bb
> 
> As far as I understand it, it will allow overloading on constants for manual constant folding & optimization. So if you have:
> 
> void foo(static int i);
> void foo(int i);
> 
> foo(3); // Calls the first
> foo(x); // Calls teh second (assuming x is not compile-time resolvable)

Yes, means you can do compile-time regexps with identical syntax to run-time 
regexps. This was my proposal from the distant past, and I think someone else 
suggested it as well. But, with macros, most of the impetus is gone; you could 
get the same effect in another way:

macro foo(i)
{
    static if (__traits(compiletimeconst, i))
          foo_template!(i);
    else foo_function(i);
}

One problem with the proposal, is that you can't currently accept any type as a 
template value parameter (only ints, floats, and strings). So
struct Bar {
  int a;
}

foo(static Bar b);

is not going to work. So it's not clear that it's a big win any more. I don't 
think we need it.



More information about the Digitalmars-d mailing list