The inline specifier might increase the code size, but it might also reduce it.
It depends on the size of the inlined function versus the overhead of setting up a stack frame and invoking the call/return sequence. Often, the inline specifier is used for very short, usually one line functions, and the intent is to sacrifice a bit of code size for execution size.
Keep in mind that the inline specifier is only a compiler hint, and that the compiler may or may not actually inline the function, depending on context.
Outline is the opposite of inline. An inline expanded function is any function or class method where the declaration also provides the definition (the implementation). This is known as implicit inline expansion. Where the definition is kept separate from the declaration, you may use the inline keyword to specifiy that the function should be inline. This is known as explicit inline expansion. Inline expanded functions (whether implied or explicit) does NOT mean the function will in fact be inline expanded. It is merely a suggestion to the compiler. If the compiler's optimisers determine that there is no advantage to be gained by inline expanding a particular function, then that function becomes an outline function. Inline expansion simply means that the body of the function is inserted in place of the function call. Function calls are expensive in terms of memory and performance, so by eliminating the function call completely, your code performs faster and uses less memory. However, functions that are called many times throughout your code will result in a much larger code size, and large code runs slower than small code. Thus the benefit of eliminating a function call has to be weighed against the increased code size. Although some compilers do allow you to override the compiler's inline expansion optimisers, this is strictly non-standard. The best judge of what to expand and what not to expand is best left in the hands of the compiler, and indiscriminate use of the inline keyword should generally be avoided.
Trivial functions, such as member variable accessors that simply return a member's value, are prime candidates for inline expansion. However trivial non-member functions can also be inline expanded, as can any non-trivial function that is rarely called.Member functions defined in the body of the class declaration are implicitly declared inline. However, whether a function is explicitly declared inline or not, the compiler is free to ignore any inline request, such as when the inline expansion of a non-trivial function would adversely compromise code size, for instance.Note that inline expansion replaces the call to a function with a modified version of the function's body within the calling functions -- just as if you'd duplicated the code yourself, rather than creating a separate function -- which removes the overhead of making a function call.The only way to force a function inline is to manually write the expanded code yourself. But if the code appears in several places, maintenance of the code will be compromised.If there's ever any doubt, declare it inline and let the compiler decide. It's in a far better position to determine if it should be inline expanded or not.
The advantage of an inline function is that it eliminates the function calls completely, thus improving the performance wherever those calls appear. The disadvantage is that it increases code size, which can be detrimental to performance. For this reason, declaring a function for inline expansion is merely a hint to the compiler. If the increased code size would be detrimental, the compiler is free to ignore the inline declaration and retain the function call instead. While the programmer is free to manually expand their own functions, this only serves to increase maintenance should the function ever need to be changed, and could lead to errors should those changes not be propagated correctly. The advantage of having a function, even if it is only ever called once (or from within a loop), is to simplify your code and make it easier to read and maintain. If the function is simple, or is called seldom, then it is a good candidate for expansion, but its almost always better to let the compiler decide which functions should be inline expanded.
Functions reduce code size in one of three ways, depending on the complexity of the function. Normally, a function call results in the compiler generating code to call and return from the function, but the code for the function itself is only generated once. This not only reduces duplication in your source code and thus makes your code much easier to read and maintain, it also reduces the overall size of the machine code if the code required to implement the call and return mechanism results in fewer operations than the function itself. However, the call and return mechanism has a runtime cost, reducing performance slightly. Some functions can be inline expanded (by the compiler) to the extent that the expansion results in smaller code than the normal call and return mechanism would incur. By eliminating the function call mechanism, we not only improve runtime performance, we can also reduce code size. Inline expansion does not guarantee smaller code size; in some cases we may increase the size of the code. Although increased code size can have an affect on overall performance, this has to be balanced against the performance that is gained by eliminating the function call mechanism. Finally, some functions can take advantage of compile-time computation, which is similar to inline expansion but where complex expressions can be evaluated at compile time and thus reduced to much simpler expressions. This not only reduces code size but greatly improves performance by completely eliminating unnecessary runtime operations.
it is the function declared inside the class. It is used for gaining faster speed in execution but it might give back the larger executable fileAn inline function is one that is not called, its code is inserted in the position where you make a call to it. If it is called multiple times from different locations it was be inserted in all locations, increasing the size of all functions its called from but marginally increasing speed.Take this example:inline int AddNumbers(int a,int b){return a+b;}int main(){int x = 2;int y = 3;int answer;answer = AddNumbers(x,y);answer = AddNumbers(answer,y);return 0;}When the program is compiled it would look more like this:int main(){int x = 2;int y = 3;int answer = x+y;answer += y;return 0;}
For the inline functions compiler just copies the function code in that place and when the size is too big it treats that function as ordinary function.
Outline is the opposite of inline. An inline expanded function is any function or class method where the declaration also provides the definition (the implementation). This is known as implicit inline expansion. Where the definition is kept separate from the declaration, you may use the inline keyword to specifiy that the function should be inline. This is known as explicit inline expansion. Inline expanded functions (whether implied or explicit) does NOT mean the function will in fact be inline expanded. It is merely a suggestion to the compiler. If the compiler's optimisers determine that there is no advantage to be gained by inline expanding a particular function, then that function becomes an outline function. Inline expansion simply means that the body of the function is inserted in place of the function call. Function calls are expensive in terms of memory and performance, so by eliminating the function call completely, your code performs faster and uses less memory. However, functions that are called many times throughout your code will result in a much larger code size, and large code runs slower than small code. Thus the benefit of eliminating a function call has to be weighed against the increased code size. Although some compilers do allow you to override the compiler's inline expansion optimisers, this is strictly non-standard. The best judge of what to expand and what not to expand is best left in the hands of the compiler, and indiscriminate use of the inline keyword should generally be avoided.
Trivial functions, such as member variable accessors that simply return a member's value, are prime candidates for inline expansion. However trivial non-member functions can also be inline expanded, as can any non-trivial function that is rarely called.Member functions defined in the body of the class declaration are implicitly declared inline. However, whether a function is explicitly declared inline or not, the compiler is free to ignore any inline request, such as when the inline expansion of a non-trivial function would adversely compromise code size, for instance.Note that inline expansion replaces the call to a function with a modified version of the function's body within the calling functions -- just as if you'd duplicated the code yourself, rather than creating a separate function -- which removes the overhead of making a function call.The only way to force a function inline is to manually write the expanded code yourself. But if the code appears in several places, maintenance of the code will be compromised.If there's ever any doubt, declare it inline and let the compiler decide. It's in a far better position to determine if it should be inline expanded or not.
Trivial functions, such as member variable accessors that simply return a member's value, are prime candidates for inline expansion. However trivial non-member functions can also be inline expanded, as can any non-trivial function that is rarely called.Member functions defined in the body of the class declaration are implicitly declared inline. However, whether a function is explicitly declared inline or not, the compiler is free to ignore any inline request, such as when the inline expansion of a non-trivial function would adversely compromise code size, for instance.Note that inline expansion replaces the call to a function with a modified version of the function's body within the calling functions -- just as if you'd duplicated the code yourself, rather than creating a separate function -- which removes the overhead of making a function call.The only way to force a function inline is to manually write the expanded code yourself. But if the code appears in several places, maintenance of the code will be compromised.If there's ever any doubt, declare it inline and let the compiler decide. It's in a far better position to determine if it should be inline expanded or not.
The advantage of an inline function is that it eliminates the function calls completely, thus improving the performance wherever those calls appear. The disadvantage is that it increases code size, which can be detrimental to performance. For this reason, declaring a function for inline expansion is merely a hint to the compiler. If the increased code size would be detrimental, the compiler is free to ignore the inline declaration and retain the function call instead. While the programmer is free to manually expand their own functions, this only serves to increase maintenance should the function ever need to be changed, and could lead to errors should those changes not be propagated correctly. The advantage of having a function, even if it is only ever called once (or from within a loop), is to simplify your code and make it easier to read and maintain. If the function is simple, or is called seldom, then it is a good candidate for expansion, but its almost always better to let the compiler decide which functions should be inline expanded.
Functions reduce code size in one of three ways, depending on the complexity of the function. Normally, a function call results in the compiler generating code to call and return from the function, but the code for the function itself is only generated once. This not only reduces duplication in your source code and thus makes your code much easier to read and maintain, it also reduces the overall size of the machine code if the code required to implement the call and return mechanism results in fewer operations than the function itself. However, the call and return mechanism has a runtime cost, reducing performance slightly. Some functions can be inline expanded (by the compiler) to the extent that the expansion results in smaller code than the normal call and return mechanism would incur. By eliminating the function call mechanism, we not only improve runtime performance, we can also reduce code size. Inline expansion does not guarantee smaller code size; in some cases we may increase the size of the code. Although increased code size can have an affect on overall performance, this has to be balanced against the performance that is gained by eliminating the function call mechanism. Finally, some functions can take advantage of compile-time computation, which is similar to inline expansion but where complex expressions can be evaluated at compile time and thus reduced to much simpler expressions. This not only reduces code size but greatly improves performance by completely eliminating unnecessary runtime operations.
it is the function declared inside the class. It is used for gaining faster speed in execution but it might give back the larger executable fileAn inline function is one that is not called, its code is inserted in the position where you make a call to it. If it is called multiple times from different locations it was be inserted in all locations, increasing the size of all functions its called from but marginally increasing speed.Take this example:inline int AddNumbers(int a,int b){return a+b;}int main(){int x = 2;int y = 3;int answer;answer = AddNumbers(x,y);answer = AddNumbers(answer,y);return 0;}When the program is compiled it would look more like this:int main(){int x = 2;int y = 3;int answer = x+y;answer += y;return 0;}
An inline function replaces the call to the function by the body of the function, thus reducing the overhead of saving the context in stack. This is good for functions which are small in size and called occasionally. A recursive function calls an instance of itself and thus can be a deeply nested. Different compilers handle this differently. Some will inline it up to a certain depth and then call a non-inlined instance for further recursion; others will not inline the function at all and generate a normal function call.
Please ask only one question at a time. A class is the definition of a type. Classes encapsulate data and the methods that work upon that data into a single entity. They can be thought of as database records with built-in methods that operate upon the fields. However, classes can restrict access to the data and methods, thus hiding information that needn't be exposed outside of the class. The class interface defines how users may interact with the class. An object is simply an instance of a class. To understand the difference, consider the following: int x; An int is a type, while x is an instance of the type. Instances of a class are no different: class foo{}; // declaration of a class type named foo. foo bar; Here, foo is the type and bar is an instance of the type. bar is therefore an object, of type foo. An inline function is a function that is declared inline. This tells the compiler that the function is a candidate for inline expansion, meaning all calls to that function can be replaced with inline code, eliminating the function calls completely but at the expense of increased code size. Not all functions are suitable candidates for inline expansion and the compiler is free to ignore the directive if it determines expansion would be detrimental to performance. While eliminating function calls improves performance of the function, the increased code size may be detrimental to overall performance. The compiler attempts to strike a balance between the two, based upon the compilation flags specified. For small code, inline expansion is largely ignored for all but the simplest and smallest of functions. For fast code, inline expansion is applied to many more functions that are declared inline. Large and complex functions are usually ignored unless they are called from just a few unique places in your code. In general, inline expansion is really only suitable for functions with few statements. Declaring every function inline is not recommended as it increases the workload placed upon the compiler. Recursive functions should never be declared inline as it is impossible to expand a recursive function without knowing the depth of recursion beforehand. The merit of a non-inline function call is smaller code size at the expense of performance. If a function is only called once, it would be better to declare it inline, regardless of its complexity. If it is called many times from many places, and is relatively simple, inline expansion could improve performance at the expense of code size. However, the compiler is the best judge, so if you think a simple function is a candidate for expansion, or you know that a complex function is only called once, then declare it inline. You are free to manually inline expand functions if you wish, but this increases maintenance if the function is called from many places in your code. The advantage of retaining the function (whether declared inline or not) is that the function's code is all in one place and it makes the code that uses the function much easier to read and understand.
Inline functions effectively remove the function call altogether. However, just because a function is declared inline is no guarantee it will be inline expanded. it is merely a hint to the compiler that the function should considered for inline expansion, nothing more. To understand how inline expansion helps, consider the following code: void print(int x) { std::cout << x << std::endl; } int main() { int x=5; print(x); x*=3; print(x); print(++x); return(0); } If the print() function could be inline expanded, it would be the same as if you'd written the following: int main() { int x=5; std::cout << x << std::endl; x*=3; std::cout << x << std::endl; std::cout << ++x << std::endl; return(0); } Our code may be a little larger, but it will execute that much faster because we've completely eliminated all three function calls. But while we could expand functions inline manually, as shown above, it makes no sense to do so. In a larger program where the function might be called several dozen times, it becomes that much harder to manage the inline code because there are so many instances of it. By keeping the function and declaring it inline, we can easily modify the function's behaviour because the code is all in one place. More fundamentally, however, if we manually expand functions inline, our code becomes that much larger, and performance is directly related to code size. By giving the compiler hints as to which functions should be inline expanded, we allow the compiler to decide which is better in each case. The compiler uses complex algorithms to determine the best compromise between code size and performance. A function that is called many times but has little actual code is an ideal candidate for inline expansion, but then so is any function, regardless of complexity, that is called just the once. In the latter case the function may not be absolutely necessary, but functions can help make the code that uses them that little bit more readable and easier to understand, with much less need for commentary. Even recursive functions can be inline expanded, but usually there is a limit to the depth of recursion (16 being a common limit). This can greatly increase the performance of the function at the expense of code size, which may affect other areas of the program. However, it is not something that should concern the programmer. The compiler is a better judge of what should be expanded and what should not. Even so, some compilers also include a non-standard force_inline flag that can override the compiler's decision, but this is best used sparingly, and only when the programmer is absolutely sure the performance gained will not cause problems elsewhere due to the increased code size.
The normal way a function works is that whenever your code encounters a call to the function, it jumps to the body of the function code. An inline function tells the compiler that it should actually copy over the code from a function body into all places where that function is called. In some cases this can cause a dramatic reduction in run time, but in others it causes nothing more than increasing the size of the produced executable. Function overloading refers to the ability to have multiple functions with the same name, but different parameter types.
No. The inline keyword simply tells the compiler that the function is a candidate for inline expansion. If the compiler's optimisers approve inline expansion, the function body is inline expanded at each call site, thus completely eliminating the overhead of the function calls at the expense of increased code size. If the increased code size would be detrimental to performance, the inline expansion is ignored completely. Note that functions that are defined within their own declarations are implicitly marked for inline expansion, thus the inline keyword should only be used where interfaces are declared separately (usually in header files) from their implementations (usually in source files). Also note that inline expansion is only suitable for small functions with one or two simple statements at most, or larger functions that are seldom called. Recursive functions can also be inline expanded, however the compiler will limit the depth of the calls. Any subsequent recursions will be treated as being standard function calls. However, most compilers also make use of tail recursion optimisers to minimise call depths.