0

If a private c++ method is implemented inside a .cpp compilation unit (not implemented in the class definition, nor declared as inline), is it possible for the compiler to automatically inline it anyway since it is only used inside the one .cpp compilation unit?

Example:

MyClass.h:

class MyClass
{
    public:
       void foo();

    private:
       void bar();
}

MyClass.cpp:

void MyClass::foo()
{
   bar();  //can the compiler choose to inline this call?
}

void MyClass::bar()
{
   printf("bar called\n");
}
KyleL
  • 1,379
  • 2
  • 13
  • 35
  • See [here](https://stackoverflow.com/questions/1204975/does-the-compiler-decide-when-to-inline-my-functions-in-c) and [here](https://stackoverflow.com/questions/1443982/when-do-compilers-inline-c-code) – Cory Kramer Sep 24 '15 at 14:00
  • It may be able to using `LTO` (Link Time Optimization): https://gcc-python-plugin.readthedocs.org/en/latest/lto.html – Galik Sep 24 '15 at 14:03
  • @Galik so it's not possible for the compiler itself to inline this? – KyleL Sep 24 '15 at 14:04
  • Yes of course, that's almost trivial (totally trivial if the definition of bar() came before its use). More interesting is the case when bar is in a different compilation unit. Even then, with additional stages, smarter linkers and object file annotation, it is possible these days. – Peter - Reinstate Monica Sep 24 '15 at 14:05

0 Answers0