How to require operator overloading in interface

Adam D. Ruppe destructionator at gmail.com
Fri Jul 19 07:28:42 PDT 2013


You can't have templates in interfaces unless they are final, 
otherwise it won't work right.

The way I'd do it is is make the op template final, and have it 
forward to another normal virtual function:

interface Addable {
    final Addable opBinary(string op : "+")(Addable rhs) {
         return this.opAdd(rhs);
    }

    /* virtual */ Addable opAdd(Addable rhs);
}

class Test : Addable {
     override Addable opAdd(Addable rhs) {
             return new Test();
     }
}

void main() {
    auto test = new Test();
    auto test2 = new Test();

    auto sum = test + test2;
}


More information about the Digitalmars-d-learn mailing list