If I mark any function as inline, is there a way I can know if the function gets inlined or not?
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
2010-04-06 15:23:25
@Rakesh: no it won't.
MSalters
2010-04-06 15:24:17
@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
2010-04-06 15:42:04
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
2010-04-06 15:46:23
@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
2010-04-06 15:55:15
@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
2010-04-06 16:32:56
@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
2010-04-06 17:46:51
+5
A:
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.
anon
2010-04-06 15:12:28
+17
A:
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.
Nikolai N Fetissov
2010-04-06 15:13:35