templates and scoping

Ellery Newcomer ellery-newcomer at utulsa.edu
Sat Apr 3 10:47:17 PDT 2010


On 04/01/2010 12:38 PM, BCS wrote:
> Hello Ellery,
>
>> this is going to be a bit of a nebulous question, but
>>
>> if I define a template
>>
>> template T(string s){
>> }
>> where s is an identifier, then is there a good way to branch T's
>> behavior based on whether s is declared in the scope in which T gets
>> instantiated?
>>
>
> some combination of "is()" "typeof" and "mixin()" should do it.
>
> //untested
>
> static if( mixin("is("~s~")") ) { ... }
>

All right, here's a first try:

//// test.d
module test;
import tok;

int a; //1
void main(){
     int a; //2
     void dummy(){};
     mixin T!("a");
}


//// tok.d
module tok;

int a; //3
template T(char[] k){
         static if (is(typeof(mixin(k)))){
             pragma(msg,"true");
             static if(is(typeof(mixin("."~k)))){
                 pragma(msg,"uh oh");
             }
             const K = true;
         }else{
             pragma(msg,"false");
             const K = false;
         }
}


The problem I see is it can't distinguish between 1 or 2 and 3


More information about the Digitalmars-d-learn mailing list