When should I write the keyword 'inline' for a function/method?
Solution 1
Oh man, one of my pet peeves.
inline
is more like static
or extern
than a directive telling the compiler to inline your functions. extern
, static
, inline
are linkage directives, used almost exclusively by the linker, not the compiler.
It is said that inline
hints to the compiler that you think the function should be inlined. That may have been true in 1998, but a decade later the compiler needs no such hints. Not to mention humans are usually wrong when it comes to optimizing code, so most compilers flat out ignore the 'hint'.
static
- the variable/function name cannot be used in other translation units. Linker needs to make sure it doesn't accidentally use a statically defined variable/function from another translation unit.extern
- use this variable/function name in this translation unit but don't complain if it isn't defined. The linker will sort it out and make sure all the code that tried to use some extern symbol has its address.inline
- this function will be defined in multiple translation units, don't worry about it. The linker needs to make sure all translation units use a single instance of the variable/function.
Note: Generally, declaring templates inline
is pointless, as they have the linkage semantics of inline
already. However, explicit specialization and instantiation of templates require inline
to be used.
Specific answers to your questions:
-
When should I write the keyword 'inline' for a function/method in C++?
Only when you want the function to be defined in a header. More exactly only when the function's definition can show up in multiple translation units. It's a good idea to define small (as in one liner) functions in the header file as it gives the compiler more information to work with while optimizing your code. It also increases compilation time.
-
When should I not write the keyword 'inline' for a function/method in C++?
Don't add inline just because you think your code will run faster if the compiler inlines it.
-
When will the compiler not know when to make a function/method 'inline'?
Generally, the compiler will be able to do this better than you. However, the compiler doesn't have the option to inline code if it doesn't have the function definition. In maximally optimized code usually all
private
methods are inlined whether you ask for it or not.As an aside to prevent inlining in GCC, use
__attribute__(( noinline ))
, and in Visual Studio, use__declspec(noinline)
. -
Does it matter if an application is multithreaded when one writes 'inline' for a function/method?
Multithreading doesn't affect inlining in any way.
Solution 2
I'd like to contribute to all of the great answers in this thread with a convincing example to disperse any remaining misunderstanding.
Given two source files, such as:
-
inline111.cpp:
#include <iostream> void bar(); inline int fun() { return 111; } int main() { std::cout << "inline111: fun() = " << fun() << ", &fun = " << (void*) &fun; bar(); }
-
inline222.cpp:
#include <iostream> inline int fun() { return 222; } void bar() { std::cout << "inline222: fun() = " << fun() << ", &fun = " << (void*) &fun; }
-
Case A:
Compile:
g++ -std=c++11 inline111.cpp inline222.cpp
Output:
inline111: fun() = 111, &fun = 0x4029a0 inline222: fun() = 111, &fun = 0x4029a0
Discussion:
Even thou you ought to have identical definitions of your inline functions, C++ compiler does not flag it if that is not the case (actually, due to separate compilation it has no ways to check it). It is your own duty to ensure this!
Linker does not complain about One Definition Rule, as
fun()
is declared asinline
. However, because inline111.cpp is the first translation unit (which actually callsfun()
) processed by compiler, the compiler instantiatesfun()
upon its first call-encounter in inline111.cpp. If compiler decides not to expandfun()
upon its call from anywhere else in your program (e.g. from inline222.cpp), the call tofun()
will always be linked to its instance produced from inline111.cpp (the call tofun()
inside inline222.cpp may also produce an instance in that translation unit, but it will remain unlinked). Indeed, that is evident from the identical&fun = 0x4029a0
print-outs.Finally, despite the
inline
suggestion to the compiler to actually expand the one-linerfun()
, it ignores your suggestion completely, which is clear becausefun() = 111
in both of the lines.
-
Case B:
Compile (notice reverse order):
g++ -std=c++11 inline222.cpp inline111.cpp
Output:
inline111: fun() = 222, &fun = 0x402980 inline222: fun() = 222, &fun = 0x402980
Discussion:
This case asserts what have been discussed in Case A.
Notice an important point, that if you comment out the actual call to
fun()
in inline222.cpp (e.g. comment outcout
-statement in inline222.cpp completely) then, despite the compilation order of your translation units,fun()
will be instantiated upon it's first call encounter in inline111.cpp, resulting in print-out for Case B asinline111: fun() = 111, &fun = 0x402980
.
-
Case C:
Compile (notice -O2):
g++ -std=c++11 -O2 inline222.cpp inline111.cpp
or
g++ -std=c++11 -O2 inline111.cpp inline222.cpp
Output:
inline111: fun() = 111, &fun = 0x402900 inline222: fun() = 222, &fun = 0x402900
Discussion:
- As is described here,
-O2
optimization encourages compiler to actually expand the functions that can be inlined (Notice also that-fno-inline
is default without optimization options). As is evident from the outprint here, thefun()
has actually been inline expanded (according to its definition in that particular translation unit), resulting in two differentfun()
print-outs. Despite this, there is still only one globally linked instance offun()
(as required by the standard), as is evident from identical&fun
print-out.
- As is described here,
Solution 3
You still need to explicitly inline your function when doing template specialization (if specialization is in .h file)
Solution 4
1) Nowadays, pretty much never. If it's a good idea to inline a function, the compiler will do it without your help.
2) Always. See #1.
(Edited to reflect that you broke your question into two questions...)
Solution 5
When should I not write the keyword 'inline' for a function/method in C++?
If the function is declared in the header and defined in the .cpp
file, you should not write the keyword.
When will the the compiler not know when to make a function/method 'inline'?
There is no such situation. The compiler cannot make a function inline. All it can do is to inline some or all calls to the function. It can't do so if it hasn't got the code of the function (in that case the linker needs to do it if it is able to do so).
Does it matter if an application is multithreaded when one writes 'inline' for a function/method?
No, that does not matter at all.
Partial
Updated on August 31, 2022Comments
-
Partial almost 2 years
When should I write the keyword
inline
for a function/method in C++?After seeing some answers, some related questions:
When should I not write the keyword 'inline' for a function/method in C++?
When will the compiler not know when to make a function/method 'inline'?
Does it matter if an application is multithreaded when one writes 'inline' for a function/method?
-
Mark Byers over 14 yearsYes. The inline is only a hint to the compiler, and it is free to ignore you. These days the compiler probably knows better than the programmer which functions are best to inline.
-
endorphin over 14 yearsYes, but it's less relevant - for a function to be inlined, it's body must be in the same compilation unit (for instance, in a header). That's less common in C programs.
-
Partial over 14 yearsWhile the compiler can do a good job today, do you know of any cases when you should write it?
-
Pavel Minaev over 14 years
inline
functions are typically not inlined unless compiling with optimizations, so they do not affect debugging in any way. Remember that it's a hint, not a demand. -
Pavel Minaev over 14 yearsThe problem is that
inline
has a semantic difference in C++ (e.g. in the way multiple definitions are treated), which is important in some cases (e.g. templates). -
deft_code over 14 yearsgcc by default does not inline any functions when compiling without optimization enabled. I don't know about visual studio
-
wallyk over 14 yearsI worked on an enormous g++ project which had debugging enabled. Maybe other options prevented it, but the
inline
functions were inlined. It was impossible to set a meaningful breakpoint in them. -
deft_code over 14 yearsdefining a non-member function template (aka non-static function template) does not require inline. See one definition rule(3.2/5).
-
deft_code over 14 yearsinline is used to resolve cases where a symbol has multiple definitions. Templates however are already handled by the language. One exception is a specialized template function that doesn't have any template paramters anymore (template<>). These are treated more like functions than templates and so need the inline keyword in order to link.
-
deft_code over 14 yearsenabling debugging doesn't stop inlining in gcc. If any optimization where enabled (-O1 or greater), then gcc will try to inline the most obvious cases. Traditionally GDB has had a hard time with breakpoints and constructors especially inline constructors. But, that has been fixed in recent versions (at least 6.7, maybe sooner).
-
Martin York over 14 years+1 Best description of inline I have seen in ... (forever). I will now rip you off and use this in all my explanations of the inline keyword.
-
David Thornley over 14 yearsAdding
inline
will do nothing to improve the code on a modern compiler, which can figure out whether to inline or not on its own. -
user2918201 almost 13 yearsDoes your answer to "When should I not write..." essentially mean, "if you think a function should be inline it shouldn't." Are your saying that we should inline one liners as a rule and not think too much about the rest?
-
deft_code almost 13 years@Ziggy, what I was trying to say was that compiler inlining and the
inline
keyword are not related. You've got the right idea though. As a rule, guessing what would would be improved by inlining is very error prone. The exception to that rule being one liners. -
user673679 about 11 yearsThis answer confuses me a bit. You say all that about the compiler being able to inline / not inline things better. Then you say that you should put one liners / small functions in the header, and that the compiler can't inline code without the function definition. Aren't these a bit contradictory? Why not just put everything in the cpp file and let the compiler decide?
-
deft_code about 11 yearsThe compiler will only inline function calls where the definition is available at the call site. Leaving all function in the cpp file would limit inlining to that file. I suggest defining small one liners inline in the .h as the cost to compilation speed is negligible and you're almost guaranteed the compiler will inline the call. My point about compiler inlining is that it is port of the black art of optimization, at which your compiler is much better than you are.
-
Phlucious about 11 yearsIs this answer still true? The idea that compilers pretty much do their own thing when inlining seems to fly in the face of the internet's cumulative knowledge. I like the idea of not inlining any more because of circular dependencies, but I won't tolerate losing performance.
-
IInspectable almost 11 yearsWhenever I read something to the account of the internet's cumulative knowledge I have to think of John Lawton's famous quote: The irony of the Information Age is that it has given new respectability to uninformed opinion.
-
sasha.sochka almost 11 yearsVery nice answers but I still can't get how compiler will be able to inline a function if it doesn't see it's definition. Let's imagine we have main.cpp, func.cpp, func.h. In main we call
func()
. main.cpp is a separate compilation unit so it has no idea about func definition. Another situtation - what happens (yes, it's bad design) if class definition is divided into 2 separate files. Then how will private methods be inlined if compiler sees no their definition anymore? -
Renaud almost 11 years"However, the compiler doesn't have the option to inline code if it doesn't have the function definition." Recent linker/compilier can do link time optimization and can inline function later if necessary.
-
legends2k over 10 years@deft_code: +1 for clarity in words, but what's confusing is that in the standard §7.1.2/3 footnote says
The inline keyword has no effect on the linkage of a function.
. Am I missing something here. -
deft_code over 10 years@legends2k: linkage here is referring to a symbol can be used outside its compilation unit. This aspect of linking is controlled by
extern
(the default ... usually) andstatic
. It is well defined to have astatic inline
orextern inline
function. -
deft_code over 10 years@Imray, it looks like the official term in the standard is "translation unit". I use the two interchangeably, but that may not be correct. see What is a translation unit for more info.
-
paulm over 10 yearsmsvc also has __declspec(forceinline)
-
paulm over 10 yearsEnable /Wall to be told about which functions where marked inline but didn't actually get inlined
-
void.pointer over 10 yearsYour suggestion on using the 'inline' keyword seems over-complicated. I honestly feel like you could completely strip the inline keyword from the language and nothing would change. If you implement it in the header, the compiler will make the decision on its own; regardless of what you tell it.
-
deft_code over 10 years@RobertDailey, C++ has no module support yet. The compiler for the most has no idea what a header is. When C++ gets good module support, you'll be correct that much(all?) of
inline
s semantics will not be needed. -
void.pointer about 10 yearsI'd like to see more evidence to back up your claims. Please provide code you are testing with as well as assembler output with and without inline keyword. Any number of things could have given you performance benefits.
-
void.pointer about 10 yearsAlso this answer seems a bit incorrect since section [7.1.2] in the C++03 standard states
"...The inline specifier indicates to the implementation that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism..."
So it's not just for scoping (even though I completely agree, the standard does make it pretty clear that it's more than that). -
Sharp Steel Software about 10 yearsAh, the pearls of C++ wisdom and mythology that very few will ever know. It's like the keyword was named
inline
solely to confuse the masses and keep the true meaning hidden for a select royal few. "Don't add inline when you think your code will run faster if the compiler inlines it." -- This was hilarious and very counter-intuitive, and also 100% correct :D -
Cahit Burak Küçüksütcü over 9 yearsSo why do professionals use FORCEINLINE. As an example, UE4 source code has a lot of inline methods. Why do they let compiler do inlining?
-
Melebius over 9 years-1:
inline
is still needed, for example to define a function in a header file (and that is required for inlining such a function in several compilation units). -
Étienne about 9 years@deft_code " It's a good idea to define small (as in one liner) functions in the header file" Modern compilers can use Link Time Opmization/Whole Program Optimization, when this is used defining a function in a header file makes no difference regarding inlining.
-
Étienne about 9 years@Melebius : A function can be inlined in several compilation units without defining a function in a header file, and without using inline, the compiler has to be configured for that (In GCC this is called link time optimization, in visual studio whole program optimization).
-
underscore_d over 7 years"inline is just a request to compiler similar to register" They're similar because neither are requests or have anything to do with optimisation.
inline
has lost its status as an optimisation hint, and most compilers only use it to make allowances for multiple definitions - as IMO they should. More so, since C++11,register
has fully been deprecated for its prior meaning of 'I know better than the compiler how to optimise': it's now just a reserved word with no current meaning. -
Ruslan over 7 years@Étienne that's implementation-specific. Per standard, there's One Definition Rule, which means here that if you naively include the function definition in multiple translation units, you'll get an error. But if that function has
inline
specifier, its instances are automagically collapsed into one by the linker, and ODR isn't used. -
MikeMB over 7 years@Etienne: Sure it does. Just because the linker can do inlining at lunchtime doesn't necessarily mean it will make the same decision, as the compiler would have. And btw.: GCC's inliner still does consider the inline keyword when making it's decision. It is not difficult to come up with an example, where versions with and without will result in different assembler code.
-
MikeMB over 7 yearsFinally someone who doesn't only repeat what others say, but does actually verify those statements. Gcc does indeed still consider the inline keyword as a hint (I think clang ignores it completely).
-
MikeMB over 7 years@void.pointer: Why is this so hard to believe? If optimizers were perfect already, then new versions couldn't improve the program performance. But they regularly do.
-
MikeMB over 7 years@underscore_d: Gcc still listens to
inline
to some degree. -
Droj over 7 years"the compiler doesn't have the option to inline code if it doesn't have the function definition", so if you want the compiler to have the option to inline, then you should put it in the header, which means it may be included by multiple compilation units, requiring you to use 'inline'. Alas, have we not come full circle by saying "use 'inline' to allow the compiler to inline"? (...or maybe I'm just missing it :))
-
Jean-Simon Brochu about 7 yearsI have optimized several algorithms by simply adding "inline" where my profiler told me there was a bottle neck. I don't think that all compilers can always outperform human intervention. This answer is misleading and incomplete. Please refer to stackoverflow.com/questions/1932311/…
-
Jean-Michaël Celerier over 6 years"so most compilers flat out ignore the 'hint'." This is patently false. At least Clang and GCC use the inline keyword as a hint for inlining: blog.tartanllama.xyz/inline-hints
-
R Sahu about 6 yearsYour answer is an illustrative post of why language makes such
inline
functions to be undefined behavior. -
deft_code about 6 yearsLLVM will inline functions costing less than 225 or 325 if hinted. For scale a 7 case switch statement "costs" 110, devirtualization saves 100 cost. So hints do effect the compilers' inlining decisions and more than I thought (naively one would expect up to 50% more inlining if all functions were hinted). The intent of the ignore statement is to point out that the compiler inlines many functions w/o the hint and ignores complex hinted functions. I'll update the the comment to be more clear.
-
syockit over 4 yearsYou ought to also add cases where compiling and linking is separate, with each
.cpp
being its own translation unit. Preferably, add cases for-flto
enabled/disabled. -
gast128 over 4 yearsI have a different observation. Using inline does make a difference across translation units. Example of two simple get functions in release mode with VS2017: the one with inline specifier gets inlined and the other is just a normal function call. Things become more obscure though using /LTCG or DLL module boundary.
-
Petr Fiedler over 4 yearsThe C++ reference explicitly sais "If an inline function or variable (since C++17) with external linkage is defined differently in different translation units, the behavior is undefined.". So the stuff you wrote is GCC specific as it is a side effect of orchestration of the compilation and linkage processes. Also, notice that this might vary between versions.
-
Van Tr over 4 years"only when the function's definition can show up in multiple translation units." is there any use case that you need this one ?
-
Robin Davies about 4 yearsThere are cases where it is appropriate to use inline in a .cpp file. E.g. applying optimizations to code that is entirely implementation specific.
-
Johannes Schaub - litb about 4 years@RobinDavies updated answer. It seems you misunderstood what I wanted to write.
-
HTNW about 4 years@Trevor An important one in modern C++ is
constexpr
, which impliesinline
. The compiler can't evaluate a function without actually having it's definition, so you need it to beinline
as every TU that uses it must have the definition. -
Lewis Kelsey about 4 yearsIt may benefit you to know that this isn't actually the case. The optimisation level -O0 through - Ofast is what determines whether a function is inlined or not. Inline on regular compilation (-O0) will not inline a function regardless of whether you use
inline
or not in C and C++. C Inline: stackoverflow.com/a/62287072/7194773 C++ inline: stackoverflow.com/a/62230963/7194773 -
Henrik Alsing Friberg about 4 yearsI get that
inline
tells the linker to allow symbol collisions (sticking to the symbol from the first translation unit), but why on earth is it not required to test the symbols for equivalence? The standard should require compilers to provide LTO-information for all inline functions and make such checks mandatory! -
001001 almost 4 yearsWhy does the MS C++ docs docs.microsoft.com/en-us/cpp/cpp/… say "The inline and __inline specifiers instruct the compiler to insert a copy of the function body into each place the function is called." if (now 20 years later), compilers ignore it?
-
frankelot almost 4 yearsHow did this answer get so many upvotes? It confused me more that I already was
-
Abhishek Mane about 3 years@deft_code you said that " you should use
inline
keyword when the function's definition can show up in multiple translation units andJohannes Schaub - litb
said that in his answer(bottom of this page) " If the function is declared in the header and defined in the .cpp file, you should not use theinline
keyword." so I usedinline
keyword while declaring the function in.h
header file and I define it in .cpp file but it give error such asundefined reference
so I feeljohannes Schaub - litb
is correct but I am not sure so can you please put some light on this. -
Abhishek Mane about 3 years@JohannesSchaub-litb If the function is declared in the header and defined in the .cpp file, then you should not use the
inline
keyword. butdeft_code
(967 upvotes and Accepted answer) mention opposite to that you should only useinline
keyword when the function's definition can show up in multiple translation units so I checked it by declaring function in header file with keywordinline
and defining it in .cpp file, it gives an errorundefined reference
. so you are right. Now also you mentioned,..........continue in next comment -
Abhishek Mane about 3 years@JohannesSchaub-litb ........ code of function in multiple translation unit is not available to compiler so it can't make them inline so it's linkers job . in this sense,
deft_code
says that so you should useinline
keyword so it gives compiler more info. to work with an optimizing code so his wording also makes sense here but when I try to use in code as mentioned early it gives error . so I feel both of your statements are opposite to each other but both of them makes sense but when I check practically your statements is true , so can you please put some light on this. -
QuentinUK over 2 yearsAlthough the compiler "can" inline functions in .cpp files automatically with GCC you need to tell it with flags such as -flto ( link-time optimiser )(and compile all source files ( and libraries ) with this flag ) otherwise it won't ( or just for those source files compiled with the flag ).