how to pass a variable name in the template in this case?

Ali Çehreli acehreli at yahoo.com
Thu Aug 2 16:56:30 PDT 2012


On 08/02/2012 04:42 PM, Zhenya wrote:
 > I mean that in ths code
 >
 > double f = 0;
 >
 > template T(alias a)
 > {
 > void doit()
 > {
 > a = 1;
 > }
 > }
 >
 > int main(string[] argv)
 > {
 > T!f.doit();
 > writeln(f);//alias a is'nt 0,alias a is f

Yes, the a in template T is an alias of f.

 > So why if we declared variable whith name 'f' something should change?

Getting back to your original code, the problem with the following line:

template T(alias a)
{
     auto T = a;    // <-- Error
}

Since templates are compile-time features, T!f must be evaluated at 
compile time and the template's value must be equal to the alias a.

But you are not instantiating the template with a compile-time entity:

     char f = 'a';
     writeln(typeid(T!f)); // <-- can't work

You must make f evaluatable at compile-time:

import std.stdio;

template T(alias a)
{
     auto T = a;
}

int main(string[] argv)
{
     enum f = 'a';      // <-- enum works
     assert(T!f == 'a');

     return 0;
}

Another option is to define f as immutable:

     immutable f = 'a';

Ali



More information about the Digitalmars-d-learn mailing list