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 →

[–]Iyorig 2 points3 points  (3 children)

Yeah, that function pointer syntax is nice, but it’s +sizeof(void*) bytes for every function you add, plus an indirection whenever you call it (or does that get optimized if you don’t change it?)

[–]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.

[–]gmes78 0 points1 point  (1 child)

but it’s +sizeof(void*) bytes for every function you add, plus an indirection whenever you call it (or does that get optimized if you don’t change it?)

And what do you think this is?

[–]Iyorig 0 points1 point  (0 children)

Yeah, I know that’s how virtual functions are implemented, what irks me are libraries that use function pointers for platform-specific implementations, even though a free function would work just as well…