Using template mixin, with or without mixin ?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 7 16:53:12 PDT 2017


On 04/07/2017 04:47 PM, biocyberman wrote:
> I want to use mixin to generate function in-place. In template
> declaration, I can see 'mixin' keyword is optional. Is it true? What is
> the difference and when I must use one way over another?
>
> This is my program:
>
> // This works with and without 'mixin' attribute.
> mixin template funcgen(T, U){
>
>   T func1(string pr2){
>     writeln("Func1: ", pr2);
>   }
>   U func2(string pr3){
>     writeln("Func2: ", pr3);
>   }
>
> }
>
> int main(string[] args){
>
>   mixin funcgen!(void, void);
>   func1("func1");
>   func2("func2");
>   return 0;
>
> }

The difference is that you can't use funcgen as a regular template:

     funcgen!(void, void);

Error: template instance funcgen!(void, void) mixin templates are not 
regular templates

I think it's good practice to use 'mixin template' if it's intended to 
be so.

Ali



More information about the Digitalmars-d-learn mailing list