Why do std.traits use template foo(args...) instead of foo(alias arg) ?

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Dec 18 08:21:33 PST 2014


On Thu, 18 Dec 2014 15:52:06 +0000
Low Functioning via Digitalmars-d-learn
<digitalmars-d-learn at puremagic.com> wrote:

> On Thursday, 18 December 2014 at 15:48:02 UTC, Mathias LANG wrote:
> > An exemple being fullyQualifiedName:
> > https://github.com/D-Programming-Language/phobos/blob/master/std/traits.d#L415
> >
> > I don't get this pattern. Is it documented somewhere ?
> 
> http://dlang.org/variadic-function-templates.html
that's not a question about "what it does?", but the question about
"why it did this way?"

the answer is simple: `alias` arguments can't accept types. i.e.

  template t0(T...) if (T.length == 1) {
    enum t0 = T[0].stringof;
  }

  template t1(alias T) {
    enum t1 = T.stringof;
  }


  pragma(msg, t0!int);
  pragma(msg, t1!int);

# dmd -c -o- test.d

  int
  test.d(11): Error: template instance t1!int does not match template declaration t1(alias T)
  test.d(11):        while evaluating pragma(msg, t1!int)

but:

  int a;
  pragma(msg, t0!a);
  pragma(msg, t1!a);

gives:

  a
  a

i.e. (T...) can accept both types and symbols, and `alias` can accept
only symbols. `fullyQualifiedName` then checks if T is symbol or type:

  static if (is(T)) ... // T is a type, process as type
  else ... // T is a symbold, process as symbol
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20141218/d42f4027/attachment.sig>


More information about the Digitalmars-d-learn mailing list