Home c++ Inline functions in C and C++

Inline functions in C and C++




What is the difference between inline functions in C and C++? Desirable with reference to the standard.

Answer 1, authority 100%

In both C and C++, the inline keyword does not guarantee that the function will be inlined in the calling code, but is merely a wish to the compiler that this function should be called as quickly as possible. Therefore, the only tangible effect of the inline keyword is how it affects the rules for declaring and defining functions.

If we consider functions with internal binding, i.e. functions declared as static inline , then there is actually no difference between C and C++ (if I am not missing anything).

However, when it comes to functions with external binding, the difference between languages ​​is pretty significant.

  • In C++, the rules are simple: multiple definitions of inline functions are allowed (in different translation units). Moreover, if a function is declared inline in one translation unit, then in all other translation units where it is declared, it must be declared exactly inline . In all translation units where this function is defined, it must be defined in the same way.

  • In the C language, there is a rather confusing division between inline function definitions and external function definitions.

    • Inline definition occurs when, in a given translation unit, all declarations of a given function are made with the keyword inline , but none of them contain the keyword extern . In such a situation, the function definition does not create an external symbol – it cannot be linked to from another object file.

      It is forbidden to define mutable static objects and thread-local objects in inline-definitions. Also from there you cannot refer to entities (objects and functions) with internal linkage.

    • External definition occurs when a given translation unit either has a “normal” function declaration (without inline ), or a declaration with two keywords at once extern inline . An external definition is a normal function – it generates an external symbol to which you can link from another object file – just make a declaration of this function there.
      & nbsp;

    For example

    inline void foo (); // Announcement
    inline void bar (); // Announcement
    inline void foo () // Definition
     static int i = 42;
    inline void bar () // Definition
     static int j = 42; // Error!
    void foo (); // Announcement
    inline void bar (); // Announcement

    In this example, the definition of the bar function is erroneous, because it is an inline definition, and modifiable static objects cannot be defined in inline definitions.

    At the same time, the definition of the function foo is an external-definition because below in the text you will find the declaration of this function without the word inline . There are no restrictions on this definition.


    • If in some translation unit there is an inline definition of a function, and there is no extern definition of this function anywhere in the project, then the inline definition will be called.

    • If in some translation unit there is an inline definition of a function, and somewhere in the project there is an extern definition of this function, then the compiler has the right to choose which definition to call.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions