Should operator overload methods be virtual?

Walter Bright newshound1 at digitalmars.com
Sat Nov 28 13:31:47 PST 2009


retard wrote:
> Is this again one of those features that is supposed to hide the fact 
> that dmd & optlink toolchain sucks? At least gcc can optimize the calls 
> in most cases where the operator is defined to be virtual, but is used in 
> non-polymorphic manner.

The gnu linker (ld) does not do any optimizations of virtual call => 
direct call. Optlink has nothing to do with it.

   struct C {
     virtual int foo() { return 3; }
   };

   void bar(C* c) {
     c->foo();   <== no virtual call optimization here (1)
   }

   int main() {
     C* c = new C();
     c->foo();   <== virtual call optimization here (2)
     bar(c);
     return 0;
   }

What D doesn't do is (2). What D does do, and C++ does not, is allow one 
to specify a class is final or a method is final, and then both (1) and 
(2) will be optimized to direct calls.

Doing (2) is entirely a function of the front end, not the linker.



More information about the Digitalmars-d mailing list