Hi, I'd like to understand how inline functions work.
I've read that you have to fully define the functions you want to inline before you call them.
That's because if I understand it correctly the compiler has to generate the specific inline code to create the obj at compile time so it has to know the function definition at compile time. Code compiled in other .cpp files can't be linked later because is not meant to be inline but it's based on the usual function call system.
Then why if I write this in Visual 2013:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
sum.cpp
int sum(int a, int b)
{
return a + b;
}
main.cpp
#include <iostream>
usingnamespace std;
inlineint sum(int, int);
int main()
{
cout <<"The sum of 2+5 is: "<< somma(2, 5)<<endl;
system("pause");
}
it works perfectly? The function is not defined in main.cpp but just declared to be used inline, the actual code is compiled in sum.cpp and linked later.
Should I assume that while I'm not receiving an error the compiler is definetly NOT using an inline version of the function sum since ''inline'' should be considered just a suggestion and not an order?
According to what I've read the right code should be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
sum.h
inlineint sum(int a, int b)
{
return a + b;
}
main.cpp
#include <iostream>
usingnamespace std;
#include "sum.h"
int main()
{
cout <<"The sum of 2+5 is: "<< sum(2, 5)<<endl;
system("pause");
}
I don't know where you got your information from, but it's either wrong or you're misreading it.
Decades ago, inline was a hint to the compiler to perform an optimization of replacing calls to the function with the function body itself - it increases size, but can also increase speed. In today's world though, it is completely ignored - compilers always make the decision for you whether or not the inline keyword is there. For a function to be inlined, in most cases the compiler just needs to have access to the function body at compile time (not link time).
The other use of the keyword is to allow you to define a function body in a header file (it will allow multiple definitions).
Frank,
your understanding appears correct to me.
Your first example will never be inlined because:
1. the actual code is in sum.cpp so isnt available to inline while compiling main.cpp
2. even if it was in the .h, your inline is on your declaration when it needs to be on the definition.
Your example of the correct code appears correct to me.
as you say, its just a suggestion for the compiler, and it may or may not agree with you.
@LB, I've never heard of a compiler IGNORING a directive. However, because the compiler is free to make its own decision regardless of the coders "suggested inline" then the suggestion is worthless as it can never force its opinion on the compiler. So i guess using the keyword itself is meaningless now.
What do you mean by "it will allow multiple definitions"?
you dont need to make a declaration for inline function.
1. it is good if the inline function has only short code or situation
2. it executes quicker than a normal function.
but the disadvantage is it takes big memory on your program.
Without the inline keyword the code would compile but fail to link.
ah, but thats not multiple definitions is it, there is still only one definition in source code and zero in runtime (inlined)
I appreciate that without the inline there is the potential for giving multiple definitions to the linker. but if the compiler ignores inline directives then the point is surely moot and the compiler would have to force all definitions in headers to be inline regardless, just to avoid the multiple definitions problem?
If you place function definition in header file and include it in several other source files, you have multiple definition.
but if the compiler ignores inline directives
It does not. And it cannot, or it will be nonconforming. What is can do, it is to decide to not inline function in place where asked, if it thinks that it would do more harm than good.
the compiler would have to force all definitions in headers to be inline regardless
Compiler does not know what "header" is. At all. It is merely called that for convenience. All what compiler operates on are large preprocessed files.
Stuff like #include directives are processed by preprocessor before compiler would see and parse file.
Frank,
your understanding appears correct to me.
Your first example will never be inlined because:
1. the actual code is in sum.cpp so isnt available to inline while compiling main.cpp
2. even if it was in the .h, your inline is on your declaration when it needs to be on the definition.
Your example of the correct code appears correct to me.
as you say, its just a suggestion for the compiler, and it may or may not agree with you.
Thank you!
That was my interpretation as well. My doubt was that I was just expecting the compiler to generate some kind of alert because I explicitly ''suggested'' to inline a function but my wrong code isn't putting the compiler in a position to meet my request even if it wanted to. So it's normal that it doesn't generate alerts because inline is a suggestion anyway, it will simply never inline my function (in my bad code example) and that's it.
@LB: My source is the Deitel C++ book (which should be a highly respectable source on the matter...or at least I hope so :P).
They say about inline functions "Complete definition of the function is required to appear before it's used in the program so that the compiler knows how to expand a function call into its inlined code".
Actually when you say:
LB wrote:
For a function to be inlined, in most cases the compiler just needs to have access to the function body at compile time (not link time).
You're basically saying the same thing because if the compiler needs to have access to the function body at compile time, you need to include the full body (with the inline request) in the .cpp where it's called. If the body is defined in an other .cpp the function won't be available at compile time but will be linked later hence it can't be inlined because the code in other .cpp isn't inlined but compiled as a call to subroutine.
By the way you're all suggesting that a compiler can decide by itself to inline a fully declared function if the conditions are met, even without the inline keyword....so inline is pretty useless anyway? :D
I think this is an issue of misunderstanding confusing wording. The inline keyword will not result in a compilation error if you try to use the function without the full definition in scope. It just means it's not possible for the inlining optimization to take place.
The inline keyword is almost useless now, it just has the remaining use I mentioned earlier. In terms of hinting to the compiler that you would like a function inlined, you may as well act to increase the lifespan of your keyboard.
I think this is an issue of misunderstanding confusing wording. The inline keyword will not result in a compilation error if you try to use the function without the full definition in scope. It just means it's not possible for the inlining optimization to take place.
The inline keyword is almost useless now, it just has the remaining use I mentioned earlier. In terms of hinting to the compiler that you would like a function inlined, you may as well act to increase the lifespan of your keyboard.
That was my doubt. Why if I'm making an explicit inline request but at the same time I'm not writing code which puts the compiler in a position to meet my request I don't get even just an alert like "You're trying to inline something that can't be inlined"?
I think I got that the answer is that since inline has just a suggestion nature, the compiler doesn't need to generate any alert, it just won't inline the function when it can't or doesn't want to.
So I guess that I could mark the thread as solved :D
Thanks to all the people who replied.