Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline in source file

Tags:

c++

inline

I have a big class with lots of utility functions. those functions are very small and I would like them inlined.

The problem is they are all in a source file and should stay in the source file and not move to the header file (so I don't need to recompile everything every time one changes).

If I mark them as inline I get

symbols not found

Is there a way to make them inline or do I need to blindly trust the link time optimizer?
I need the code to be portable between clang 3 and gcc 4.6, but #defines based on compiler will be ok (so answer how to do it only in one of the compilers is fine too).

like image 686
Dani Avatar asked Oct 24 '11 23:10

Dani


2 Answers

[These] functions are very small and I would like them inlined. [But] I don't [want] to recompile everything every time one changes.

You can't have both of these things. If a function is inlined, then you have no choice but to recompile all its callers when it changes. That's how inlining works. Even if you used a link-time optimizer to do it automatically at link time, you would still be paying the compilation-time cost of reprocessing all the callers.

AFAIK neither gcc 4.6 nor clang 3 have link-time optimizers that are up to scratch, by the way.

Editorial aside: No compiler that I know of has heuristics that are good enough to make manual inline annotations unnecessary, yet. Even VS2010, which I mentioned in the comments as an example of a link-time optimizer that is up to scratch, still needs quite a bit of advice about what to inline.

like image 58
zwol Avatar answered Sep 28 '22 16:09

zwol


Place the implementation in an .inl file. Have only the necessary .cpp files #include it. This way changes to the implementation (touch .inl file) trigger recompile only of the dependent .cpp file. Changes to the definition (touch the .h file) trigger recompile of all files consuming the declaration.

This is a common practice for both inline functions and for template implementations. .inl files should be viewed basically as 'included cpp' files.

like image 20
Remus Rusanu Avatar answered Sep 28 '22 16:09

Remus Rusanu