Why do library functions often have an inline specifier, and in "normal" code you will not find it?

  • 2
    I say as an olympiad, inline is used, but compilers usually use it on the drum, you need to use force_inline :) And it is rarely used because smart compilers will figure out what to do and how best to do it. - pavel
  • 2
    I confirm. inline is a relic of those times when compilers were stupid and when it was thought that a programmer understands machine optimization better than a compiler. The same applies to the word register, which is now generally ignored or not working. Now, almost 90% of the time, the compiler knows better whether to do the Inline function or not. - Zealint pm
  • @Zealint as a whole, yes, but sometimes magic with registers inline, frauds with memory leaks make it possible to speed up the percent code by 20 which was just not enough) - pavel
  • @pavel, show me an example? - ixSci
  • one
    @pavel, these lines are understandable, you just killed the allocation / release of memory, which is a very difficult operation. - ixSci 4:14 pm

3 answers 3

The inline does not mean that the function will be embedded at the call point instead of the actual call. At present, the compiler is in charge of such optimizations and only he, he can embed or not embed any function, without any connection with the presence or absence of inline .

The real meaning of inline this: tell the linker that functions with the same name, which he sees in different translation units, are one and the same function, and there is no need to issue a multiple definition error. Thus, with inline you can define a function in a header. Notice that the class methods defined within the class have (implicit) inline .

Library functions contain inline in order to avoid defining the function body in a cpp-file, and thus the library will not be able to contain implementation files at all.

  • "class methods defined inside a class have (implicit) inline" what is the result in practice? - Vladimir Gamalyan
  • @VladimirGamalian: The fact that the methods defined within the class defined in the header do not cause a multiple definition error. - VladD pm
  • one
    @pavel: Whether the compiler embeds a function into a call point, it does depend on the complexity of the function. (And now compilers have become much smarter, and they embed much more aggressive.) And the inline can be set regardless of the content of the function. - VladD 4:06 pm
  • one
    @ixSci: There is a chance that it was not a bug in the compiler, but UB, and the ability of the compiler to inline the function simply triggered an undefined behavior. Something on the topic: blogs.msdn.microsoft.com/oldnewthing/20140627-00/?p=633 - VladD
  • one
    Of course, the question of C ++, but perhaps it is interesting that in C, regardless of the presence of inline , you still need to add __attribute__((weak)) to the "like" functions in different files (in a function declaration, otherwise the linker swears - multiple definition ) - avp

It probably depends on whose code you are looking at.

I often write static inline ... when I want to show ( first of all to other programmers, an absolutely optional word for the inline compiler, just hint ) that, for efficiency reasons, I would like to see this code inserted by the place of use (similar to the code defined in #define ), but at the same time I would like it to be free from the side effects of expanding macro arguments (from general considerations of possible call options).

    The inline determiner tells the compiler to insert the function code at the place from which it is called. for example

     inline int quadrat(int x) { return x*x; } int main() { int y = quadrat(2); return 0; } 

    This code will be compiled into:

     int main() { int y = 2*2; return 0; } 

    As a rule, you can specify the inline determinant function only if the function body is defined in the header file ( .h ). If the function body is defined in the .c file, then the compiler will ignore this qualifier, and the function will be compiled as usual.

    • one
      “Indication”, it is too loudly said, “request”, it would be much more accurate. - ixSci
    • @ixSci and perhaps the compiler can ignore the inline? - Flowneee
    • one
      @Flowneee, of course: “It’s not a problem .” “ IxSci