What is probably happening is that the template instantiation is happening separately for class A and B, thus ending in two void f(int) functions.
This does not happen in D since there the compiler knows about the void f(int) function as a specialization and therefore does not specialize T for int.