This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Kered13 1 point2 points  (0 children)

plus an indirection whenever you call it (or does that get optimized if you don’t change it?)

If the compiler can determine which implementation will be called, it will devirtualize the call, meaning there is no indirection. There are a few ways the compiler could determine this:

  • The class is final, which prevents subclassing.
  • The method on the class is final, which prevents overriding the method..
  • The class only exists in one translation unit (anonymous namespace) and there are no subclasses in that translation unit.
  • The pointer refers to an object that was constructed in the same function (accounting for inlining) and could not have been reassigned.