Possible Duplicate:
c++ inline function?
What is the the real concept of inline function.
i really unable to understand the inline function.
why & where should i use inline function? How it is differ from normal function?
Edit: what is difference between macro & inline function?
The main language difference between an inline and a non-inline function is that inline functions are exempt from the one-definition rule, provided all definitions are the same.
This is a crucial feature for C++, since it allows you to write member function definitions inside class definitions and still be able to include the class definition in a header file.
Consider this header:
// stupid.h
#ifndef H_STUPID
#define H_STUPID
int foo() { return 8; }
#endif
stupid.h is not usable if you have to include it more than once, because you'll end up with multiple definitions of foo
. Making the declaration inline
lets you get around this problem. Applying the same logic to a class definition (remember that all member functions that are defined inline are implicitly declared inline
), this allows us to write this:
// works.h
#ifndef H_WORKS
#define H_WORKS
class Foo
{
int n;
public:
void f() { n *= 2; } // implicitly inline!
int g() const { return n; } // ditto
};
#endif
We can include works.h in as many translation units as we like, and there's no "multiple definition" error for Foo::f
and Foo::g
, because those are (implicitly) declared inline.
Of course inline
also serves as a hint to the compiler to replace function calls by copies of the function body, but the compiler can choose to do or not do that pretty much independent of whether or not you declare a function inline
.
A non-inline function compiles to an actual call of that function. An inline function is basically copied inplace (inline) so there's no call and no symbol in the symbol table.
As such, an inline function must be declared in a header file since once it's compiled, there are no references to it. You cannot link to it from another object file, for example.
Usually, inline functions are slightly faster than non-inline functions since there's no function call anymore. Useful for getters/setters.
In various versions of the C and C++ programming languages, an inline function is a function upon which the compiler has been requested to perform inline expansion. In other words, the programmer has requested that the compiler insert the complete body of the function in every place that the function is called, rather than generating code to call the function in the one place it is defined. (However, compilers are not obligated to respect this request.)
For more information and when/How to use, see this 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. Instead of transferring control to and from the function code segment, a modified copy of the function body may be substituted directly for the function call. In this way, the performance overhead of a function call is avoided.
If you use a normal function like so:
inline int max(int a, int b)
{
return (a > b) ? a : b;
}
you call the function like so:
a = max(x, y);
But a inline function is directly copied so it would be like this:
a = (x > y) ? x : y;
You should use an inline function when you wish to optimize function calls. You may consider an inline function as a macro that copies the function contents (inline) to code segment where the function is called. However, inline function is very different from a macro (google it up). Some major differences:
- Inline function is type safe
- It is up to the compiler to choose to inline the inline function or not
- Expression passed to inline functions are evaluated exactly once
Functions defined in the class definition are also inline although there is no explicit inline directive.