This question already has answers here:
Benefits of inline functions in C++?
(14 answers)
Closed 8 years ago.
I learned that the functions in class must be 'inline' from a book
But, I can't understand why...
That book wasn't enough
I'm not native speaker in English, and so I'm very poor at searching in web described by Engilsh
And my country's web sites is very closed to students like me
So please answer me
I think
If a function(not inline) were in a class, the function' s stackframe (some adresses, arguments and so on) would be created repeatedly whenever compiler refer to that function
so this is why funtions in a class must be inline?
Sorry for my poor English
In the definition of C++, if a member function is declared inside the class it is understood by the compiler as being inline
Notice that inline (including the implicit one for functions declared inside class) is just a hint to the compiler, which may or may not actually inline that function on some (or all or none) of its call sites. Inlining is always an optimization which the compiler is free to implement or not, and it often enables other optimizations too.
BTW, some compilers may inline functions not marked as inline, and that may be even done across compilation units with the so called link-time-optimization (for g++ compile and link with -flto -O2)
Related
This question already has answers here:
How will i know whether inline function is actually replaced at the place where it is called or not?
(10 answers)
Closed 6 years ago.
How can I verify whether a function is expanded inline in a C++ program?
My Compiler is Emscripten but an answer for g++ probably works.
Ideally, a code that runs differently when in inline mode (although it should not have any side effects).
You can enable the -Winline warning which prints a warning when a function marked inline wasn't inlined.
See the documentation.
As an alternative, you can mark the function always_inline which will trigger an error if it wasn't inlined. See the documentation.
This question already has answers here:
Replacement for deprecated register keyword C++ 11
(3 answers)
Closed 7 years ago.
I was reading this and it says that the register keyword will most probably be removed from the next C++ standard. It also says that register was deprecated in 2011. So, what's wrong with register storage class specifier?
I think modern compilers are very smart and they implicitly optimize frequently used variables for speed (fast access) and puts them in CPU registers.
However, C++ experts also say don't or never use register. As such, what's the problem with the register keyword?
You've pretty much answered your own question:
I think modern compilers are very smart so they implicitly optimizes frequently used variables for speed (fast access) & puts them in CPU register.
That's precisely the point—optimisers are so good at register allocation nowadays that any attempt from the programmer to enforce their will through the register keyword would likely lead to a pessimisatin, and is therefore simply ignored by the compiler. Remember that register was never a binding requirement, always just a hint to the compiler. Now that they rightfully scoff at such hints, the keyword is simply obsolete and useless.
So, to directly answer your question of "what's wrong with it:" it no longer serves any purpose whatsoever, as the only one it ever had ("hint to the compiler to put this thing in a register") is now superseded by the compilers being way better at this than humans.
Standard doesn't require that register variable to be put into registers, instead it's just a hint for compiler for variables that are often used. And compiler can determine it on its own.
Here, clause regarding register keyword from the link you posted:
A register specifier is a hint to the implementation that the variable so declared will be heavily used. [ Note: The hint can be ignored and in most implementations it will be ignored if the address of the variable is taken. This use is deprecated (see D.2). -- end note ]
This question already has answers here:
When to use the inline function and when not to use it?
(14 answers)
When should I write the keyword 'inline' for a function/method?
(16 answers)
Closed 7 years ago.
I was reading this. It says that
Define functions inline only when they are small, say, 10 lines or
less.
But Bjarne Stroustrup in his book Programming Principles and Practices using C++ says that:
Section 9.4: Defining member functions
"The obvious rule of thumb is: Don't put member function bodies in
the class declaration unless you know that you need the performance
boost from inlining tiny functions. Large functions, say five lines of
code, don't benefit from inlining. We rarely inline a function that
consists of more than one or two expressions.
So is it appropriate & helpful to define a function having 10 lines or at least 10 lines as inline. Doesn't that make program executable file size larger or even compiler can ignore the request for inlining such big function?
Is Google C++ style gives incorrect guidelines about usage of inline functions in C++?
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
When should I write the keyword 'inline' for a function/method?
So this is a question that has bugged me for a while and I can't get a definitive answer. My understanding is that a good compiler will generally realise when it is both safe and advantageous to in-line a function and, if optimisation is switched on, it will in-line all such functions weather they they are explicitly identified as in-line functions by the programmer or not. Also, a complier will recognise when it is not safe/sensible to in-line a function and will simply ignore the programmers request to in-line functions in such cases.
Thus, I would like to know what is the advantage of explicitly stating a function as in-line? As long as optimisation is switched on the compiler will in-line all the functions it deems sensible to in-line, and only those functions.
I have found some discussions around inline protecting against multiple definitions due to nested h files, but surely #ifdefine'ing the header source code is better practice and again renders the use of the key word inline void?
You're spot on about the compiler optimizations. You're just wrong in your assumption of what inline is. Despite the name inline is not for optimization. inline is primarily to "violate" the one definition rule with impunity. Basically, it tells the linker that many translation units can see that definition, so it should not barf on finding it on multiple translation units.
Some compilers may treat it as a hint to inline the function, but that's totally up to the compiler and perfectly valid to just ignore that hint.
Header guards only protect against multiple definitions on the same translation unit. They do not work across translation units.
Header guards don't protect against multiple definition errors.
Multiple definitions are encountered by the linker, and occur when the same definition is included into separate compilation units.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Inline functions vs Preprocessor macros
hello can somebody please explain what exactly does it mean, and what is the difference from regular macro(I know that it works during compile time and not preprocessor but so what?) thanks in advance for any help, looked in google but didn't find something understandable
(Assuming here that you're talking about C/C++.)
An inline function has its code copied into the points where it's called, much like a macro would.
The big reason you would use inline functions rather than macros to accomplish this is that the macro language is much weaker than actual C/C++ code; it's harder to write understandable, re-usable, non-buggy macros. A macro doesn't create a lexical scope, so variables in one can collide with those already in scope where it's used. It doesn't type-check its arguments. It's easy to introduce unexpected syntactic errors, since all a macro does is basically search-and-replace.
Also, IIRC, the compiler can choose to ignore an inline directive if it thinks that's really boneheaded; it can't do that with a macro.
Or, to rephrase this in a more opinionated and short way: macros (in C/C++, not, say, Lisp dialects) are an awful kludge, inline functions let you not use them.
Also, keep in mind that it's often not a great idea to mark a function as inline. Compilers will generally inline or not as they see fit; by marking a function inline, you're taking over responsibility for a low-level function that most of the time, the compiler's going to know more about than you are.
There is a big difference between macros and inline functions:
Inline is only a hint to the compiler
that function might be inlined. It
does not guarantee it will be.
Compiler might inline functions not
marked with inline.
Macro invocation does not perform
type checking, so macros are not type
safe.
Using function instead of a macro is
more likely to give you a nice and
understandable compiler output in
case there is some error.
It is easier to debug functions,
using macros complicates debugging a
lot. Most compilers will give you an
option of enabling or disabling
inlining, this is not possible with
macros.
The general rule in C++ is that macros should be avoided whenever possible.