Default Template Instantiation

Heromyth bitworld at qq.com
Thu May 17 08:37:01 UTC 2018


On Monday, 19 September 2016 at 22:59:53 UTC, Jonathan Marler 
wrote:
> On Monday, 19 September 2016 at 22:17:34 UTC, Mathias Lang 
> wrote:
>> 2016-09-19 23:18 GMT+02:00 Jonathan Marler via Digitalmars-d < 
>> digitalmars-d at puremagic.com>:
>>
>>>[...]
>>
>> No you can't. The example is wrong, but Stefan is right. 
>> Consider:
>>
>> ```
>> template Foo (T = string) { }
>>
>> template TakesAlias(alias Sym) {}
>>
>> alias TakesAlias!(Foo) Bar;
>> ```
>>
>> In this context, is `TakesAlias` instantiated with the 
>> template or the template instantiation ?
>>
>> It is essentially the same category of problems as when trying 
>> to use a parameterless-functions, sometime you end up calling 
>> it because of the optional parenthesis. Except that for 
>> functions we can use `&` or `typeof`.
>
> Good example, thanks for the information.

Maybe the compiler can do more works to make the code more 
readable. Here are my examples:

template EventHandler(T=Object)
{
    alias EventHandler = void delegate(T sender);
}


void test01(EventHandler handler) // Error
{
   // It's what I want. However, it doesn't work.
}

void test02(EventHandler!() handler)
{
   // It works. Howerve, it ...
}

void test03()(EventHandler handler)
{
   // It works too. Howerve, it ...
}

void test04(EventHandler!string handler)
{
   // It's OK.
}





More information about the Digitalmars-d mailing list