Can C++ compilers optimize repeated virtual function calls on the same pointer? [duplicate]
问题 This question already has answers here : Hoisting the dynamic type out of a loop (a.k.a. doing Java the C++ way) (4 answers) Closed 6 years ago . Suppose I have the following code void f(PolymorphicType *p) { for (int i = 0; i < 1000; ++i) { p->virtualMethod(something); } } Will the compiler's generated code dereference p 's vtable entry for virtualMethod 1 or 1000 times? I am using Microsoft's compiler. edit here is the generated assembly for the real-world case I'm looking at. line-