template magic

Simen kjaeraas simen.kjaras at gmail.com
Tue Jan 25 07:33:06 PST 2011


spir <denis.spir at gmail.com> wrote:

> 1. Is the fact that we have so many ways to define the same thing a Good  
> Thing, according to you?

Yes.


> 2. What is the reason for Phobos defining param funcs as template  
> params? Efficiency? Why?

Efficiency is one reason, as DMD is unable to inline functions passed as
parameters. It is also a kind of currying, allowing one to define the
function as foo!bar, and for instance store it in an alias.


> 3. What is the meaning of 'alias f'? How does it work? This is a totally  
> enigmatic feature for me. Seems it allows placing anything of any type  
> as template param. Far more versatile than common use of templates as  
> 'simple' generics. Note that if I remove the seemingly useless 'alias'  
> from version 1 , I get:
>      Error: template instance hof1!(int,f) does not match template  
> declaration hof1(P,f)
> ???
> (could not find any explanation on alias params anywhere -- pointers  
> also welcome)

D templates take three kinds of parameters - types, aliases, and values.
While the other two are easy to understand, aliases are somewhat more
complex. In short, an alias can reference anything that has a name. The
details may be more complex, but that's the gist of it.

-- 
Simen


More information about the Digitalmars-d-learn mailing list