1

Usually I prefer to have totally split declarations and definitions, therefore, I tend to define inline methods in the .cpp
I just hope that the link optimizer inline it when it is used in a different compilation unit.

My questions are:

  1. Is it really discouraged to inline in cpp if I know my linker support inline at link time?
  2. What should I do if I want to call the function from another library?(Will it work?)
Mario Corchero
  • 5,257
  • 5
  • 33
  • 59
  • Related: http://stackoverflow.com/questions/5057021/why-are-c-inline-functions-in-the-header and http://stackoverflow.com/questions/3540931/inline-functions-in-c – CB Bailey Mar 11 '13 at 21:13

1 Answers1

5

If you mark your function inline then you have to provide a definition in every translation unit in which it is used. This is incompatible with putting the definition in a single .cpp.

If you want to place a function in a single .cpp you should not declare it inline. If you want the function to be inlined you must rely on your toolchain, passing suitable optimization flags to your compiler and linker. Which functions your toolchain will actually inline is something with which you will have to experiment.

Depending on your toolchain, you may be able to decorate your functions with implementation specific attributes to indicate which functions you want to be inlined.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656