None static member functions implicitly have the this
pointer. So these inline functions will not be replaced by compiler when they are called outside the class, am I right? This seems that usage of class inline functions is very restrictive!

- 27,591
- 48
- 66
- 103

- 561
- 4
- 9
-
C doesn't have classes, so that's a done deal. I can't make much sense out of the question though, could you clarify, with code samples maybe? – Quentin Sep 22 '17 at 09:05
-
2Why do you think that having the implicit `this` argument has something to do with inlining? – HolyBlackCat Sep 22 '17 at 09:09
-
2The compiler can inline any function it wants, as long as it ensures information is passed around correctly and the code behaves as required. What a given compiler does, however, depends on the compiler. – Peter Sep 22 '17 at 09:17
-
1This logic is false. There is no connection between the first sentence and the second. The former does not imply/cause the latter. – Maxim Egorushkin Sep 22 '17 at 09:25
2 Answers
When an inline member function is called the compiler does what it does for any function call: prepare the arguments and invoke the function. Any function call is subject to inlining. Some functions cannot be inlined (e.g. functions using alloca
or vararg functions, functions whose definitions are not available), but member functions do not prevent inlining.

- 131,725
- 17
- 180
- 271
-
1You mean that "some functions would not be inlined". A very clever compiler could in theory inline a function using `alloca` or `va_arg`; in *practice* [GCC](http://gcc.gnu.org/) is not able *yet* to do that, however with a lot of work you might make it do that if you really wanted to (e.g. by spending months to code a GCC plugin doing so) – Basile Starynkevitch Sep 22 '17 at 09:41
-
@BasileStarynkevitch It could in theory. However, I thought that it is worth mentioning some of the existing limitations. – Maxim Egorushkin Sep 22 '17 at 09:45
An optimizing compiler can inline as it wishes.
For example, you might compile and link an entire program with g++ -flto -O2
(link-time whole program optimization option of GCC...).
(caveat: using g++ -flto -O2
would slow down significantly your build process; basically everything is compiled nearly "twice", once at "compile" time, once at "link" time, where GIMPLE representation gets re-optimized)
You'll be surprised by what function calls get inlined (a lot of them could be). It is unrelated to static or not functions marked or not as inline
.
So you should not care about inlining (it is an implementation and optimization detail), but you hope that the compiler will do a good job.
(sometimes with g++
you want to disable most inlining to ease debugging with gdb
; then compile with g++ -fno-inline -Wall -Wextra -O0 -g
)
So these inline functions will not be replaced by compiler when they are called outside the class, am I right?
You are wrong, this
is just an implicit argument (see also that), and of course compilers are often inlining calls to member functions. In practice C++ would be inefficient if compilers did not that optimization (since many member functions -e.g. getters and setters- are quite short and quick).
Remember that C++ is a specification written in some report (it is not a compiler). Inlining is a quality of implementation issue, and may or not happen.
If you care about what do the compiler really does (and you should not care, but you need to avoid undefined behavior in your code), ask it to show the generated assembler code. With GCC compile with g++ -O2 -fverbose-asm -S
to get a foo.s
assembler file from a foo.cc
translation unit.

- 223,805
- 18
- 296
- 547
-
You can use `-fno-fat-lto-objects` to prevent double compilation. – Maxim Egorushkin Sep 22 '17 at 09:44
-
-
-