The function is defined as

inline void func() { // тело функции } 

I noticed that not always the function is really "inline". That is, the compiler somehow decides when to embed, and when not to embed.

    4 answers 4

    The fact is that inline , in fact, is not intended to embed functions. It is used to declare functions in h-files.

    “Making” the compiler inline a function, in fact, there is no universal way, because The standard final solution is left to the compiler. Personally, I do not know a way to force gcc. For mc there is a secret keyword __forceinline .

      I absolutely agree with @kirelagin, "forcing" the compiler to do this is quite difficult. You can help him more with this. And the word inline says nothing. A more accurate way to follow a few rules when creating procedures.

      1. the procedure should be small (several lines approximately, but no one can say exactly how much)
      2. its definition must be accessible (of course, lib and dll are excluded) and close enough (in the sense of translation units) to the place of use
      3. pointers to this procedure should not be used
      4. if a procedure is a method (that is, it is defined in a class), then it should not be virtual (but there is an exception, such a procedure can also become embedded if it is called in the constructor)
      5. maybe there is something else that I don’t remember now :)

      following these simple rules and with the optimization included in the compiler there are good chances for the compiler to "inline" such a procedure

        The standard says so:

        A function declared in an inline function . The function of the specimen may appear more than once; it appeared only once. It is as fast as possible. This is where you can find out what is effective.

        Those. if it is faster not to embed a function, the compiler has the right to do so.

        From the gcc documentation:

        You can also direct all the “simple enough” functions to their choice of options.

        Try to use this key. To check if the inline function has become use the -Winline switch, then the compiler will tell you which functions are converted to built-in

        • This is "as a fast as possible." translates as: "Creating a function built-in function assumes that the function call will be as fast as possible." Those. a function call will be noticeably faster if it is inline. Those. You did not say anything new and translated is not true. - IAZ
        • @IAZ, re-read the entire paragraph. There it is written in English that the choice of how to accelerate the function call remains at the mercy of the compiler. That is, the inline keyword does not guarantee that the function will actually be inline . You can open the standard, paragraph 6.7.4, and re-read it all, the link is open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf . - saigono
        • @saigono and I really against it? only here is the thing - they have already written about it here ... You wrote: "Ie if it doesn’t build the function faster, the compiler has the right to do so." Sorry of course but this is nonsense. How can I quickly or slowly build in a function ??? - IAZ
        • But does __attribute __ ((always_inline)) not work? - stanislav
        • @ Vova with this attribute, the compiler will tell if it can not embed this function. Those. in other words, it still does not guarantee anything, but you will know what happens with the function :) - IAZ

        It all depends on the complexity of the function and its size. And it depends on the compiler. In MinGW, it’s not possible to use built-in functions at all, the compiler cursed them.

        • You probably didn’t use keys like -std = c99 or -std = gnu99 - saigono
        • one
          @saigono correctly noticed - there is a chip with the support of different standards. Either use keys or replace inline with inline . - kirelagin
        • Just used the keys because in c89 there is no inline. But inline did not use it yet. Thanks, I will try :-) - psyhitus