templates in functions

BCS ao at pathlink.com
Fri Jul 27 15:16:58 PDT 2007


Why can't templates be put into functions like this

|void foo()
|{
|  template Bar(int i){int Bar;}
|
|  Bar!(5) = 7;
|  Bar!(12) = 3;
|  writef("%s, %s\n", Bar!(5), Bar!(12));// prints "7, 3"
|}

this would be vary useful in templated code where a number of variables need 
to be declared outside of the scope where they will be used

|void foo(A...)()
|{
|  template Bar(int i){int Bar = 0;}
|
|  while(cond())
|  {
|    foreach(i, v; A)
|      Bar!(i) = A(Bar!(i)); // values not reset
|  }
|}

(in this case, trivial work around exist but I have run into cases where 
it is much harder to work out a solution)

I don't think that this would cause problems because the template could only 
be used inside the function and therefor by the end of the semantic pass 
all instances of the template would be known.

for this case a short hand syntax might even be worthwhile, but I can't think 
what it would be.

int Bar(int i) = 0;  // looks like a fn decoration
int(int i) Bar = 0;  // ??
int template(int i) Bar = 0; // just looks odd
template(int i) int Bar = 0; // template ID( args ) [ decoration | '{' decorationList 
'}' ]




More information about the Digitalmars-d-learn mailing list