I wouldn't be surprised if most C++ compilers did this kind of optimization, at least some variations of it. This is very language- and compiler-specific, actually.
I can't speak for the D language, but for C/C++, this kind of optimization above can be difficult to make because of pointer arithmetic. e.g., can you optimize the code if it's something like this instead?
++myDelegate;
myDelegate();
It depends so heavily on the type of myDelegate
. The above can be valid C/C++, but inlining myDelegate() might not be something the compiler can guarantee.
Other languages (e.g., Java, C#, etc.) don't have pointer arithmetic, so more assumptions can be made. The Sun JVM, for example, can convert indirect, polymorphic calls to direct calls, which is pretty cool, IMHO. Example:
public class A2 {
private final B2 b;
public A2(B2 b) {
this.b = b;
}
public void run() {
b.f();
}
}
public interface B2 {
public void f();
}
public class C2 implements B2 {
public void f() {
}
}
A2 a2 = new A2(new C2());
can actually be optimized, and the Sun JVM can pick that up.
I got this example from the Java Specialists newletter 157, which I recommend reading to learn about this kind of thing WRT Java.