opAdd and opAdd_r both match

Li Jie cpunion at gmail.com
Sun Dec 10 23:57:50 PST 2006


== Quote from Karen Lanrap (karen at digitaldaemon.com)'s article
> Because more than one match is a hint, that the coder might not have
> noticed, that there are indeed at least two matches, especially when
> there is a hirarchy of deriving.
> In your case one of the overloads is essentially dead code with its
> possibly harmful impact on maintenance.

Thanks.

Sometimes I need it. A new program:

--------------------
class Bar{}

class Foo{
  Bar opAdd(T)(T v){
    return null;
  }

  Bar opAdd_r(T)(T v){
    return null;
  }
}

auto foo1 = new Foo;
auto foo2 = new Foo;
auto bar1 = foo1 + 1; // OK
auto bar2 = 1 + foo1; // OK
auto bar3 = "a" + foo1; // OK
auto bar4 = [1,2,3] + foo1; // OK
auto bar5 = foo1 + foo2 // Not OK
--------------------

How to write opAdd_r? I try to write it:

--------------------
template opAdd_r(T){
  static if (!is(T == Foo)){
    Bar opAdd_r(T v){
      return null;
    }
  }
}
--------------------
Compile failed too:
zzz.d(61): template zzz.Foo.opAdd_r(T) is not a function template
zzz.d(10): template zzz.Foo.opAdd_r(T) cannot deduce template function from
argument types (Foo)


May be I need a NO-MATCH Template Specialization:
------------------
class Foo{
  Bar opAdd_r(T: !Foo)(T v){   // *(T: !Foo)*
    return null;
  }
}
------------------


More information about the Digitalmars-d-learn mailing list