This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Inlining virtual member function calls



Hi,


I wonder if the TI C++ compiler/linker is capable of inlining virtual member function calls given that only one class implementing that virtual function is instantiated in a statically linked program.


We want to write well testable code and are therefore currently concerning writing pure virtual interface classes for almost every class. We hope that we will be able to easily exchange those classes with mock-up implementations while testing. However, we are worried that we will see a massive drop in performance in our release build when enforcing this design rule because the compiler might not be able to inline most of our function calls anymore.

Is this a valid concern? If so, how would you write unit tests without making use of virtual functions?

  • The compiler does not inline calls to virtual functions.

    Well, I need to hedge that a little bit.  The compiler does try to determine what any pointer (not just function pointers) is pointing at.  Sometimes, it figures it out and replaces the pointer dereference with that piece of data or call to a function.  In theory, that could happen with the function pointer used to implement calls to virtual functions.  Once the call to the function is known, it might be inlined.  All that said, the compiler makes no explicit attempt to inline calls to virtual functions.

    Thanks and regards,

    -George 

  • If you write your code to use templates instead of virtual function calls, you might be able to get similar ease-of-testability without sacrificing performance.