template constraints when non-constrained template exists

Manu via Digitalmars-d digitalmars-d at puremagic.com
Mon Oct 5 01:14:06 PDT 2015


So, this might be a really nub question, but it's a problem that
constantly comes up, I tend to find some simple workaround, and it
goes away... but I need to understand.

template X(T)   // this template is EVERYWHERE in phobos, look in std.traits
{
  static if(isOkay(T))
  {
    ...
  }
  else
    static assert(0, "T is no good!");
}

Given this, the trouble is X(T) accepts anything, and it wasn't made
to handle a particular T, so I get the static assert.

So I want to add alongside:

template X(T) if(someCondition!T)
{
 ...
}

The idea being to handle T's that the original template fails at...
but this just doesn't seem to work.
I just get fir instance: "Error: template std.traits.Signed matches
more than one template declaration: ..."

Which is true, but shouldn't it pick the most appropriate match? In
this case, the one that matches the given constraint I would think is
a better match than the open catch-all version.

My specific problem is that I want Signed!, Unsigned!, isSigned!,
usUnsigned!, etc to work with my type. Looking at Unsigned!T for
instance, it tests UnsignedTypeOf!T, and that tests IntegralTypeOf!T

Ah-hah, surely I just need to declare IntegralTypeOf!T for my type,
and it all comes good? But nope, the problem above.

This comes up in many cases, I presume I've just missed something
really obvious...?


More information about the Digitalmars-d mailing list