Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Have read through many aricels now and do not get it, nor found a compendium. Im totally confused!
Here the rules I found out:
is that correct????

A) inline keyord now is obsolete in C++11, the Compiler always decides on ist own
C++
int add(int x) { return x+1; } 
inline int add(int x) { return x+1; }

do the same!

B) The function Body has to be placed in the class declaration OR if not a class member, in the cpp file where its used.
.h:
C++
virtual int add(int x);
.cpp
C++
int myclass::add(int x) { return x+1 }

never will be inlined ???

Visual Studio 2013, C++ and some MFC

(We have many dump functions that do nothing or only return true/false in our C++ Code)

What I have tried:

have read through many articels, web Forums but did´n get it.
And? Is there a way to find out what the Compiler did?

thanks Folks!
Posted
Updated 19-Oct-16 2:28am
v3

It is compiler dependant and also affected by code generation / optimisation options. So you have to check the compiler specific documentation. For the Microsoft compiler see Inline Functions (C++)[^].

The only reliable ways I know to check if a function has been inlined or not is inspecting the generated code (e.g. by using the assembly output using the /FA, /Fa (Listing File)[^] option or inspecting the symbol table).
 
Share this answer
 
Comments
FriendOfAsherah 19-Oct-16 3:36am    
this answers A)
will try with the Switches
B) stil open
The compiler chooses. See, for instance inline specifier - cppreference.com[^].
 
Share this answer
 
Comments
FriendOfAsherah 19-Oct-16 3:36am    
this answers A) - B) stil open
A) Inlining is code optimization, and modern compilers are already way better at code optimization than programmers*. Because of that, leaving the decision to the compiler is almost always the best option. This wasn't always the case, so, in the past, it made sense for compiler builders to ask programmers for a hint, in the form of the inline keyword. C++11 however recognizes that this is no longer sensible.

B) virtual functions can not be inlined at compile-time, because which function has to be called has to be decided at run-time.**

P.S.:
*: unless you are the programmer who implemented the compiler optimization algorithms
**: many compilers will not inline even non-virtual functions if the function implementation is placed in a separate file. The reason is that the implementation file is typically not visible to any other compilation unit, and it would take a lot of extra effort to find the implementation code every time the compiler finds a call to that function. Modern compilers still may do that, but there's no guarantee.
 
Share this answer
 
v2
Comments
FriendOfAsherah 19-Oct-16 8:21am    
sorry should be 5**** cannot change
Stefan_Lang 19-Oct-16 8:22am    
Thanks anyway. Happy to help.
OK -
A) is answered - ist Compiler Task like I guessed
B) was wether it makes sense to put short functions into declarations or not
in some forums I read that the Compiler cannot optimize when funcion is not in same file like the class declaration:

@Stefan_Lang: good Input, never read about that point of view.

Now I wrote a short test: seperated files or function Body in class declaration etc... using the Switches @Jochen Arndt told us.

At all: the Compiler does what it thinks is the best, also funbodies outside class declaration were optimized and inlined
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900