how to properly overload function templates?

bearophile bearophileHUGS at
Thu Mar 11 17:55:51 PST 2010

> Of course this can be circumvented by using
> opBinary(string op, U:Vector2)(U v)
> opBinary(string op, U:int)(U v)
> But is this how it's supposed to be done? Couldn't the compiler detect  
> that itself?

The compiler can probably do that by itself, but to do that I think templates need to change a little how they work.

That code doesn't look too much bad, I think it's acceptable. You can also use template constraints, but the code gets a little worse:

opBinary(string op, T)(T v) if (is(T == Vector2) {
opBinary(string op, T)(T v) if (is(T == int) {

Or you can squash it in a single template, but I think it's worse:

opBinary(string op, T)(T v) {
  static if (is(T == Vector2) {
  } else static if (is(T == int) {
  } else
    assert(0, "...");


More information about the Digitalmars-d-learn mailing list