4 ms·
Does declaring a function as inline do anything for any modern compiler? I understood that this is basically ignored now and is the compiler makes its own decis
by andy99 8mo ago
Does declaring a function as inline do anything for any modern compiler? I understood that this is basically ignored now and is the compiler makes its own decisions based on what is fastest.
- ddtaylor 8mo agoKind of. At the end of the day the compiler can do almost anything it wants outside of unrefined behavior, which isn't much of a guard rail. In reality header only libraries allow for deep inlining, the compiler may optimize very specifically to your code and usage. The situation is a bit more exaggerated with C++ because of templates, but there is some remaining gains to he had in C alone.
- wasmperson 8mo agoThe idea that it does nothing is a persistent myth. Both GCC and Clang heed it although neither treats it as a mandate: https://tartanllama.xyz/posts/inline-hints/ https://tartanllama.xyz/posts/inline-hints/ This library seems to have the annotation on every function, though, so it's possible the author is just following a convention of always using it for functions defined in header files (it'd be required if the functions weren't declared `static`).
- TheNewAndy 8mo agoOne obvious benefit for a header only library is that it suppresses the warning you get when a static function isn't used.
- uecker 8mo agoIt is not a benefit if you do not get warnings about unused functions. With any proper library, you would also not get warnings for functions that are part of the API that are not used, but you would get warnings about non-exported functions internal to a translation unite that are accidentally not used. This is a good thing.
- adrian_b 8mo ago"static inline" is not the same as "inline". In the former case the compiler is allowed to always inline the function. In the latter case, even when the compiler chooses to inline the function, it also emits code for an independent instance of the function, because the function is public and it may be called from another file. So "static inline" in the worst case does nothing, but it suggests to the compiler that the function should be inlined everywhere, which it will probably do, unless it decides that the function is too long (or it uses some features forbidden in inlined functions, e.g. variadic arguments, setjmp, alloca, etc.), so the benefits of inlining it may be less than the disadvantages. When the compiler refuses to follow the suggestion of inlining the function, it can be made to tell the reason, e.g. with "-Winline". So the compiler does not ignore the suggestion, even if it may choose to not follow it.
- garaetjjte 8mo ago>In the latter case, even when the compiler chooses to inline the function, it also emits code for an independent instance of the function, because the function is public and it may be called from another file. Not in standard C. "inline" function provides implementation for usage iff compiler decides to inline the call. If it does decide not to inline, it will emit call to external symbol that needs to be defined in different TU (otherwise you will get errors at link time).
- adrian_b 8mo agoThe meaning of "inline" differs between C and C++. Quote from the gcc manual: "GCC implements three different semantics of declaring a function inline. One is available with -std=gnu89 or -fgnu89-inline or when gnu_inline attribute is present on all inline declarations, another when -std=c99, -std=gnu99 or an option for a later C version is used (without -fgnu89-inline), and the third is used when compiling C++." Nevertheless, "static inline" means the same thing in all 3 standards, unlike "inline" alone. This can be a reason to always prefer "static inline", because then it does not matter whether the program is compiled as C or as C++.