Easy Tutorial
❮ Android Tutorial Togglebutton Switch Js Jquery Refresh Iframe ❯

Usage of inline in C++

Category Programming Techniques

1. Reason for Introducing the inline Keyword

In C/C++, to address the issue of small frequently called functions consuming a large amount of stack space (stack memory), the inline modifier was specifically introduced to denote inline functions.

Stack space refers to the memory allocated for local data (data within functions) of a program.

In a system, stack space is limited, and frequent large-scale usage can lead to errors due to insufficient stack space, such as infinite recursive calls of a function eventually exhausting stack memory.

Here is an example:

Example

#include <stdio.h>

inline const char *num_check(int v)
{
    return (v % 2 > 0) ? "Odd" : "Even";
}

int main(void)
{
    int i;
    for (i = 0; i < 100; i++)
        printf("%02d   %s\n", i, num_check(i));
    return 0;
}

The above example demonstrates the standard usage of inline functions. The benefit of using inline is not apparent on the surface, but internally, it replaces the function calls within each for loop with the function body, avoiding the overhead of repeatedly opening up stack memory for frequent function calls.

2. Restrictions on inline Usage

inline usage is limited. It is suitable only for functions with simple body code and cannot contain complex control structures such as while, switch, nor can the inline function be directly recursive.

3. inline is a Suggestion to the Compiler

An inline function is merely a suggestion to the compiler. Whether it will actually be inlined depends on the compiler's judgment. If the compiler deems the function simple enough to be expanded at the call site, it will inline it. Declaring a function as inline is just a suggestion.

4. Suggestion to Place inline Function Definitions in Header Files

Since an inline function needs to be expanded at the call site, the compiler must have access to the inline function's definition everywhere it is called. This requires the inline function's definition to appear in every file that calls it.

Therefore, placing the inline function's definition in a header file is appropriate, saving the trouble of implementing it in each file.

Ensure consistency between declaration and definition: If the inline function is implemented in each file, ensure all definitions are identical to avoid undefined behavior. If the definitions are not consistent across files, which definition the compiler expands depends on the specific compiler. Thus, it is best to place the inline function definition in a header file.

5. Member Functions in Classes and inline

Member functions defined within a class are implicitly inline. If the function definition is provided within the class definition, that is ideal. If the function is not defined within the class and you want it to be inline, you must add inline outside the class; otherwise, it is not considered inline.

class A
{
    public: void Foo(int x, int y) { } // Automatically an inline function
}

Although placing the member function definition within the class declaration is convenient, it is not a good programming practice. The example above should be rewritten as:

// Header file
class A
{
    public:
    void Foo(int x, int y);
}

// Definition file
inline void A::Foo(int x, int y) {}

6. inline is a "Keyword for Implementation"

The inline keyword must be used together with the function definition to make the function inline; placing inline only before the function declaration has no effect.

The following style of function Foo cannot be an inline function:

inline void Foo(int x, int y); // inline only with function declaration
void Foo(int x, int y) {}

Whereas the following style of function Foo becomes an inline function:

void Foo(int x, int y);
inline void Foo(int x, int y) {} // inline with function definition

Therefore, inline is a "keyword for implementation" rather than a "keyword for declaration." Typically, users can read the function declaration but not the definition. Although most textbooks include the inline keyword in both the declaration and definition of inline functions, it is advisable that inline should not appear in function declarations. This detail does not affect the function's functionality but reflects a fundamental principle of high-quality C++/C programming: keep declaration and definition separate; users do not need to know whether a function should be inline.

7. Use inline with Caution

Inlining can improve the execution efficiency of functions, but why not define all functions as inline functions? If all functions were inline, the keyword "inline" would be unnecessary. Inlining functions trades code bloat for the elimination of function call overhead, thereby improving execution efficiency.

Inline should not be used in the following cases: Long functions where inlining would lead to high memory consumption. Loops where the time spent executing the function body is greater than the overhead of function calls. Constructors and destructors can be misleading; they may hide behaviors such as executing constructors and destructors of base classes or member objects. Therefore, do not arbitrarily place constructor and destructor definitions within class declarations. A good compiler will automatically cancel inlining for functions that are not worth inlining (further supporting that inline should not appear in function declarations).

8. Summary

Inline functions are not a panacea for enhancing performance. They are effective only when the function is very short; however, if the function is not short and is called in many places, it will increase the size of the executable. The most frustrating issue is when the compiler refuses to inline. In older implementations, the results were unsatisfactory, although there have been significant improvements in newer ones, but they are still not perfect. Some compilers are smart enough to determine which functions can be inlined and which cannot, but most are not, so experience is needed to make the judgment. If inlining does not enhance performance, avoid using it!

>

Original link: https://www.cnblogs.com/fnlingnzb-learner/p/6423917.html

** Click to Share Notes

Cancel

-

-

-

❮ Android Tutorial Togglebutton Switch Js Jquery Refresh Iframe ❯