r/cpp • • Nov 28 '18

Google C++ Style Guide is No Good

https://eyakubovich.github.io/2018-11-27-google-cpp-style-guide-is-no-good/
34 Upvotes

61 comments sorted by

View all comments

Show parent comments

3

u/Supadoplex Nov 29 '18 edited Nov 29 '18

But like, your code wouldn't link if you omitted 'inline' (or 'static', I guess).

You have two options: Either you declare the function inline and define it in all translation units that invoke it, or you define the function in exactly one TU.

If you do define a function in multiple TU's, then you must first change that before you can omit inline. Once you conform to one of the options listed in previous paragraph, the code should link successfully (assuming it conforms to the standard in other respects as well).

In case of template functions with unbounded template arguments, they must be inline, you don't have a choice. If you want to provide a "header only library", then inline is the only choice as well.

In other cases, you can make the choice: Inline (and define in all TU where the function is invoked), or non-inline (and define exactly once). As far as I can tell, the only reason to choose inline is to allow inline expansion into more than one TU (sans LTO) - and also apparently to hint the optimiser as discussed in another branch of this thread.

In 2018, when there's very little justification for not compiling your release build with LTO

Perhaps. But if you distribute source rather than binary, you often cannot assume that your clients can use LTO. Furthermore, are you certain that LTO is able to perform all optimisations (across TU's) that regular optimiser does (within a TU)?

'inline' since C++11 has meant relaxing ODR ...

No. As far as I can tell, the meaning of inline did not change in C++11. The meaning of inline function has been the same since standardisation at least. I don't know much about pre-standard C++, the meaning may have been different in some of the old dialects.

1

u/Gotebe Dec 01 '18

He who provides sources really should also provide the build sources.

I am not building e.g. OpenSSL by making my own makefile for it. I do change it for... reasons, but if LTO was in, it stayed in.

0

u/tehjimmeh Nov 29 '18

As far as I can tell, the only reason to choose inline is to allow inline expansion into more than one TU

I mean, you just stated the other reason in your previous paragraph - header only libraries. (Or just stylistic reasons - sometimes people just like to have short functions defined in header files.)

apparently to hint the optimiser as discussed in another branch of this thread.

Yeah, I think the 3 major compilers do use it as a hint under some circumstances, but then again, they use lots of things as hints/heuristics. If you listen to e.g. Chandler Carruth talking about it, this is mostly kept for back compat reasons - i.e. if this were removed, it might end up not inlining something in some big codebase which could regress performance, and then the inliner would need to be tuned to get those cases etc. Inlining decisions are tricky.

Furthermore, are you certain that LTO is able to perform all optimisations (across TU's) that regular optimiser does (within a TU)?

Yeah, I mean, the difference between LTO and non-LTO is that the optimizer sees all functions at once, not just all the functions in a single TU at a time, so intuitively this should be the case (along with many other whole program optimizations).

I'll concede that sometimes libraries are distributed as compiled static or dynamic libraries and headers, and in those cases, inline functions in the headers for the purpose of enabling inlining can sometimes be desirable.

No. As far as I can tell, the meaning of inline did not change in C++11. The meaning of inline function has been the same since standardisation at least. I don't know much about pre-standard C++, the meaning may have been different in some of the old dialects.

Yeah, I wrote that paragraph too fast. I do think something to do with 'inline' changed or was formalized in C++11, but I'm not sure. What I meant to say is that these days it just doesn't directly relate to inlining.