I need to define inline functions to improve performance of my code. At the moment declaration of functions are in .h file and definitions are in .cpp file. I added inline keyword at the front of each declaration of functions but I am getting link error. Is possible to separate declaration and definition of inline functions ?
An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory.
The most efficient way to code an inline function is to place the inline function definition in a header file, and then include the header in any file containing a call to the function which you would like to inline.
An equivalent way to declare an inline member function is to either declare it in the class with the inline keyword (and define the function outside of its class) or to define it outside of the class declaration using the inline keyword.
The function won't be callable from other files; if another file has a definition that might be used instead. The standard is vague on this point. It says that the inline definition does not forbid an external definition elsewhere, but then that it provides an alternative to an external definition.
You can separate the declaration and definition fine, but that definition must be available in every translation unit that uses the function, e.g.:
#include <iostream>
inline void foo();
int main() {
foo();
}
inline void foo() {
std::cout << "Hi\n";
}
is perfectly legal and correct.
The exact quote from n3290 § 7.1.2.4 is:
An inline function shall be defined in every translation unit in which it is odr-used and shall have exactly the same definition in every case (3.2). [ Note: A call to the inline function may be encountered before its definition appears in the translation unit. —end note ]
Where § 3.2 basically says that it has to be identical everywhere, even overload resolutions etc.
Are you absolutely sure that making your functions 'inline' would improve your performance? I am pretty sure it will not.
The compiler is able to inline some function calls if and only if it can see the body of the inlined functions. So you need to include the body of the function as well, but if do it, you do not need to annotate your function with 'inline' because the compiler only needs the body of the function -- not your 'inline' keyword. Compilers nowadays are smart and know without your hints whether and when to inline functions. And inlining does not necessarily increase your program's performance, and it is likely to increase your executable's size.
See this article by Herb Sutter. He argues that keyword "inline" has no meaning in C++. But I disagree with him. Keyword "inline" makes one difference: you can specify the body of the inline function more than once in the program (provided that it is exactly the same definition each time) -- this is useful when putting function bodies in headers (if you need this for any reason).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With