22

If I mark any function as inline, is there a way I can know if the function gets inlined or not?

user
  • 5,335
  • 7
  • 47
  • 63
Rakesh Agarwal
  • 3,009
  • 9
  • 33
  • 40
  • 2
    Marking a function as inline probably has zero meaning to your compiler in terms of actually inlining it. The compile is much better at optimization than you or I, just write the cleanest code you can and it'll inline everything it feels should be inlined. `inline` is nowadays used more to change the behavior of the linking process. – GManNickG Apr 06 '10 at 15:13
  • Why does it matter? Is the inline-ing causing a problem? – JBRWilkinson Apr 06 '10 at 15:17
  • I do wonder about the usefulness too. Unless of course it's during a performance analysis... but I doubt it somehow. – Matthieu M. Apr 06 '10 at 15:28
  • @GMan: What do you mean? ISO/IEC 14882:2003 page 107 says that *The inline keyword has no effect on the linkage of a function.* – conio Apr 06 '10 at 15:31
  • 1
    @conio: he said the linking process, not linkage. :) The `inline` keyword tells the linker to expect to encounter multiple definitions of a symbol, which would otherwise result in an error. – jalf Apr 06 '10 at 16:08
  • For details about `inline` see e.g. here: http://stackoverflow.com/questions/1759300/c-when-should-i-write-the-keyword-inline-for-a-function-method – Georg Fritzsche Apr 06 '10 at 16:34
  • @jalf: That's one way of looking at it. But one might say that having the functions static (that is, have internal linkage) would too solve the problem of multiple definitions. Wouldn't it? – conio Apr 06 '10 at 19:33
  • @conio: That would have the same immediate consequence, yes. The difference is, every unit that has a static function will generate a separate function at link-time. That is, having `static void foo(){}` in 3 files results in 3 different functions at the end of linking. Contrarily, a linker is free to combine inline functions into one function, safely assuming they are all the same (because `inline` requires that the function definition will not change, 7.1.2/4). So having `inline void foo(){}` in 3 units will have 3 functions fed into the linker, but at the end there may be only one function. – GManNickG Apr 06 '10 at 20:19
  • @conio: Yes, that would be another way to solve the problem, but like you said yourself, `inline` doesn't affect linkage. – jalf Apr 06 '10 at 20:56
  • @GMan: Not necessarily. I think that a good optimizing compiler **may** unify the 3 `static void foo()`s if their implementations are really the same. Even if the three functions must be somewhat different (e.g. because they use static variables), a good optimizing compiler will create multi-chunk functions of them, and unify most of the shared code. – conio Apr 06 '10 at 21:09
  • @conio: I agree, as long as it behaves the same anything can happen. But in general, `static` and `inline` have different intents that overlap. – GManNickG Apr 06 '10 at 21:13

3 Answers3

28

With GCC you can use -Winline compiler option:

  -Winline  Warn if a function can not be inlined and it was declared as inline.

The man file for gcc goes on to say:

  Even with this option, the compiler will not warn about
  failures to inline functions declared in system headers.

  The compiler uses a variety of heuristics to determine whether or
  not to inline a function.  For example, the compiler takes into
  account the size of the function being inlined and the amount of
  inlining that has already been done in the current function.
  Therefore, seemingly insignificant changes in the source program
  can cause the warnings produced by -Winline to appear or disappear.
Dana the Sane
  • 14,762
  • 8
  • 58
  • 80
Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
10

Look at the assembly language that your compiler emits. For example, compiling with g++:

g++ -S -c foo.c

will create a file called foo.s containing the assembly language output. Alternatively, and once again with the GCC toolset, use objdump:

g++ -c foo.c
objdump -d foo.o

Other toolsets have similar functionality.

5

1, Look at the assembler output
2, why do you care?

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • I want to verify the following. I have a public function(marked as inline) which calls a private function. And I have a non-class function(which is not a friend of the class either) calling this public function. I want to see if the public function gets inlined or not as inlining it would give this outside function, the private access to the class members. class A { private : void privatefunc() { cout<<"\n private function " ; } public : inline void publicfunc() { cout<<"\n public function " ; privatefunc();} }; A a; a.publicfunc(); – Rakesh Agarwal Apr 06 '10 at 15:23
  • 5
    @Rakesh: The access specifiers are entirely checked at compile-time. At run-time there is no public or private any more, just bytes racing around the processor. - Besides, implementations within the class definition are "inline" anyway (because the keyword mostly means "let linker ignore multiple definitions of this (so that the function can be implemented in the header, presumably in order so it *can* be inlined)". – UncleBens Apr 06 '10 at 15:42
  • As Msalter said inline doesn't change the language in any way. The only thing it can do is speed up (or slowdown!) the program execution - and the compiler will do a better job of guessing which than you will. – Martin Beckett Apr 06 '10 at 15:46
  • @UncleBens: I don't think implementations within the class definition will be 'inlined anyways. Consider the above case where the public implementation is calling into a private implementations. Also what do you mean by saying " let linker ignore multiple definitions of this". Could you please elaborate this a bit ? – Rakesh Agarwal Apr 06 '10 at 15:55
  • @Rakesh: He didn't say they will always be inlined. For details on `inline` see e.g. here: http://stackoverflow.com/questions/1759300/c-when-should-i-write-the-keyword-inline-for-a-function-method – Georg Fritzsche Apr 06 '10 at 16:32
  • @Rakesh: In that case, both the public function and the private one will likely be inlined. The code *outside* the public function still can not access the private members, whether the public function is inlined or not. As for the multiple definitions, the compiler names functions. When the linker puts everything together, it replaces those names with addresses. If two functions both have the same name, it can't know which is the right one, unless the compiler tells it that they're all the same. – Dennis Zickefoose Apr 06 '10 at 17:46