On Tue, Aug 17, 2021 at 06:11:56PM +0000, james.p.leblanc via Digitalmars-d-learn wrote: > Evening All, > > Eponymous templates allow a nice calling syntax. For example, "foo" > here can be called without needing the exclamation mark (!) at calling > sites. We see that foo is restricting a, and b to be of the same type > ... so far, so good. > > auto foo(T)(T a, T b) { ... } > > Now, suppose I need to restrict "T" only certain subsets of variable > types.
This is exactly what "signature constraints" are for. For example: auto foo(T)(T a, T b) if (is(T == string)) // <--- this is a signature constraint { ... // deal with strings here } auto foo(T)(T a, T b) if (is(T == int)) // you can overload on signature constraints { ... // deal with ints here } auto foo(T)(T a, T b) if (is(T == float) || is(T == double)) // you can use complex conditions { ... // deal with floats or doubles here } Be aware that a signature constraint failure is not an error: the compiler simply skips that overload of the function. So if you make a mistake in a sig constraint, it may *always* fail, and the compiler may try instead to instantiate a completely unintended overload and generate an error that may have nothing to do with the actual problem. T -- Give a man a fish, and he eats once. Teach a man to fish, and he will sit forever.