Inline functions very interesting vestige, inherited from the modern world is already far bandit 90. When flourished assembler, Xi compiled very compact and small programs, when processors were weak (compared to the, now put in for example mobile telephones) and code execution time valued its weight in gold. This type of features generally used not only in C, and at the time, he justified himself dashing.
I think, It is not no secret, that old computers required for writing software for heavy computation quite the same serious approach to programming position. Unscrews and had to save on everything, otherwise runtime increases in times. It is now we Rides gigabyte toys are not particularly complaining about the speed. At that time it was extremely critical, and one way to reduce the time, just were inline (integrated) functions. Now I will try to more or less available to tell why.
So, what a normal function? Take for example a simple example – factorial calculation.
1 2 3 4 5 6 7 8 | int factorial() { int i = 0; int result = 1; for(i = 2;i < 5; i++) result *= i; return result; } |
Fairly simple factorial (5!) in the for loop, and returns the result of the function. C ++ sees this function as a unit operations, grouped in a single block. Block after the compilation is placed in a memory cell once code, and body functions (in this case cycle) nowhere else in the compiled program does not repeat. Everything is beautiful – get a piece of memory, owned program, processor which jumps if necessary from the point, where is the challenge.
1 | cout << factorial(); |
In this statement And this function is enabled, processor simply performs assembler command CALL, which will be transferred to the function address. That is. function call will cost one operator (if roughly speaking). When this, in memory (more precisely in the program stack) It is engaged in place for function parameters, if they are, and be sure to address, where the processor has jumped on the function.
Inline feature saves CPU jump into a cell, at which the function begins. The very meaning of inline It is, that instead of calling the function to substitute her body (function code) in place of, where it is called.
If so describe our factorial:
1 2 3 4 5 6 7 8 | inline int factorial() { int i = 0; int result = 1; for(i = 2; i < 5; i++) result *= i; return result; } |
So instead
1 | cout << factorial(); |
we get a reversal function in the set of operators:
1 2 3 4 5 | int i = 0; result = 1; for(i = 2; i < 5; i++) result *= i; cout << result; |
as if themselves in the code written at this point.
Accordingly, the type of code:
1 | cout << factorial() << factorial() / 5; |
turn into
1 2 3 4 5 6 7 8 9 | int i = 0; result = 1; for(i = 2; i < 5; i++) result *= i; cout << result; result = 1; for(i = 2; i < 5; i++) result *= i cout << result / 5; |
Measured philosophically – code number with inline increased function. Instead of a single call line of her body functions, the substituted instead of calling , It gave the whole 6 strings. You will be in a compiled program – the number of operators will increase many times – on many, many operators in the function body, and how much time it entered into the program.
That is. the difference between inline function and normal function – duplication of body function code everywhere, where she is involved. In normal function, its body is in a single copy in the same location within the program.
Where the benefit is you ask? Saves CPU time on the jumping place of call in the body of the function. If the function is a huge and used in several places, then inline It turns out not very profitable. but, if the function (her body) small, with a minimum number of operators, solving the problem, in the old days it was easier to give up the jump and just substitute them in the right place, as the programmer described them there.
Whatever it was, in living conditions and even on modern computers, program, using inline body substitution function instead of calling, no particular benefits. Therefore, use this kind of function is necessary infrequently. IMHO, him a place in the Museum of Fame. Although, to be honest, this approach gives the fruit, who program controllers, processors and other piece of iron. But there is its own peculiarities and its approach, and go into that right now it is not necessary. Use built-in function (inline function) or not – decide for himself programmer. From my own experience, only one – do not make it there, where it is not required for the job.
> Therefore, use this kind of function is necessary infrequently.
Use inline functions obviously have to use, perhaps, not so often. But they are massively used implicitly in determining the classes and objects are created:
– any function method, definition of MDM is within the definition of its class, implicitly receives inline qualifier.
For example, the class, defining 2D-point (on surface):
class point {
private:
float x, y;
public:
...
float operator -( const point& p ) { // расстояние 2-х 2D
float dx = ( x - p.x ), dy = ( y - p.y );
return sqrtf( dx * dx + dy * dy );
}
};
And alternative 2nd definition, very similar:
class point {
private:
float x, y;
public:
...
float operator -( const point& p ); // расстояние 2-х 2D
};
float point::operator -( const point& p ) {
float dx = ( x - p.x ), dy = ( y - p.y );
return sqrtf( dx * dx + dy * dy );
};
The 1st version of the distance between 2 points is always calculated inline, and the second – call method functions.
Of course the code displayed in the comments – that's horrible … but, I hope, it will be understood.
This is the compiler care. There is a view to the programmer rarely have to use in a domestic environment, this kind of functions.
No. This is the case, When the compiler is forbidden to interfere with the implementation of the method: if the method described within the class – inline, and if within the method only declares, and implementation or outside the class, or in a separate file (that more) – funktsimonalny challenge.
This is once again the case, when the implementation is completely under the control of the author, and the author has to know what his definition of turn back.
inline principle does not change depending on whether, OOP it is used or funktsionalke )
Why such complexity theory? I do not mind refinements, but then need another article, describes the difference between these cases and. There is no need to interfere with the entire theory in a heap – get the same MSDN.
one more “fines” It is, that is inline qualifier recommendation: the compiler uses it only if it is possible, if he considers, that it is unacceptable, it uses a function call without the fact ukedomlenya (even warning).
Example – define the factorial function so:
inline unsigned long long fact3( int n ) {
return 1 == n ? 1 : n * fact3( n - 1 );
}
Here inline will not play any role.
Well, the same thing. In today's C compiler decides. In his dlya 8086 or 80286 Not all compilers are optimized so.
Another one “trifle”: inline function qualifier wonderful work, but until then, is the definition of a function and calling are in the same code file. With the growth of the project, code is split into multiple files (Separate kompilyaiya followed by the binding of object files), and it will create enormous problems finding errors: inline functions are not suitable for external binding.
inline – is the determinant of the macro, and they are not suitable for external coupling (Linked).
> This type of features generally used not only in C, and at the time, he justified himself dashing.
In the C language standard, there are no inline keyword, where such things are implemented through macros parameterized #define.
In some later compilers C inline included, but only for pdderzhaniya sovmestimsoti with C ++.
Therefore, in principle, wrong to say, that inline borrowed from C, this is – pure C ++ acquisition.
Therefore I would here something “dashingly” beginning threw.
And I'm not alluding to the standard. I hinted at HLL. In Pascal, too, such functions exist. And in BASIC it seems to have been something like that in his later forms.
> This type of features generally used not only in C
In C, there was no key word inline from the moment, when in 1969. Dennis Ritchie began to formulate, and almost until the 2000s.
All this time, if the word appears inline C program, it would cause a syntax error.
On the question of the compatibility of C and C ++ … in relation to inline:
If you compile the code as C, but with a key -std = c89 (i.e.. стандарт ANSI 1989г.), then to just be inline syntax error. And on all earlier C programming language standards.
Use of inline in C allow only compiler option -std = c99, i.e.. this keyword may be C standard only after 1999.
Actually, article about inline functions, Unlike most articles site – unsuccessful. I think, that it should be slightly adjusted, adding and correcting considering these positions.
I, when he wrote it, I did not want to go into specific details, so as not to obscure the very essence of. No wonder the Internet has generally “mnogabukaf”, which often inflates the content in lettuce Oh-Livyo.
inline will also not work with recursive functions and the like.. Inline has many nuances .
I make inline only critical one line functions
inline is convenient when working with interrupts on controllers. There, if you move the interrupt body into a function, it can be interrupted by another interrupt since you actually exit the interrupt handler to another place. inline avoids this.