C # generators compile only once: at compile time generic. (Think about it: C # allows you to use List<T> without seeing its implementation.) Here, he sees from the where T: B clause that the parameter is B , so he calls BG .
C ++ templates are compiled every time they are called. When you enter TestG<D>() , a new copy of TestG will be compiled with T = D During the call, the compiler sees that D has its own method G and calls it.
C ++ - the equivalent of C # generic would be
template<typename T> string TestG(T t) { B& b = static_cast<B&>(t);
Others' comments regarding the use of virtual apply equally to C # and C ++. I just explain why C ++ behaves differently than C #.
source share