I was reviewing a colleague's code recently and noticed he had put the "inline" keyword in in front of a bunch of Getter functions that were defined in a class declaration.
eg
class Foo
{
public:
inline bool GetBar() const { return m_Bar; }
private:
bool m_Bar;
};
I suggested in the code review that he remove the inline keywords, as I've read in a number of different places that defining a function in the class declaration is interpreted by the compiler (MSVC in this case, but apparently part of the C++ standard) as an indication that the author wants the functions inlined. My feeling is that if the extra text doesn't serve any purpose, it's just unnecessary clutter and should be removed.
His response was as follows:
The inline keyword makes it clear to other programmers interacting with this code that these functions are/should be inlined.
Many compilers still take the inline keyword into account in this case and use it to affect (read: increase) some kind of weighting that is used to decide whether or not said function would in fact be inlined.
Having the inline keyword there means that "warn if not inlined" warnings will be triggered (if enabled) if said functions are not inlined for whatever reason.
Personally, I disagree with the first reason altogether. To me, having the functions defined in the class declaration is enough to show the intention.
I'm skeptical about the last two reasons. I can't find any information that either confirms or denies the the point about the inline keyword affecting some sort of weighting. I'm also having trouble triggering the "warn if not inlined" warning for a function defined in a class declaration.
If you've read this far, I was wondering if you might have any insight into any of the above points? Additionally, if you could point me to any relevant articles/documentation, I'd really appreciate it.
Thanks!
For the particular example you mentioned, GetBar
is implicitly inlined, which makes the inline
keyword redundant. Section 7.1.2 P3 from the C++ standard says:
A function defined within a class definition is an inline function.
Also the VC++ documentation states the same:
A class's member functions can be declared inline either by using the inline keyword or by placing the function definition within the class definition.
In general, you don't need to use the inline
keyword. A C/C++ expert once said back in 2002:
the inline keyword allows the programmer to tell the compiler something it might have a hard time figuring out automatically. However, in the future, compilers may be able to do a better job of making inline decisions than programmers. When that happens, the inline keyword might be regarded as a quaint reminder of when programmers were forced to worry about details of code generation.
You can find the complete article here. You should read the whole article to understand why the keyword was added to C99. Also the article discusses extern inline
functions.
Now we are in the future and indeed, modern compilers are very sophisticated and don't require this keyword anymore. The only exception is when using extern inline
functions.
Does the inline keyword have any affect whatsoever on the compiler's decision to inline the function?
In MSVC, the inline keyword might affect the compiler's decision, although the compiler may choose to ignore this hint if inlining would be a net loss.
The inline keyword makes it clear to other programmers interacting with this code that these functions are/should be inlined.
This is an invalid reason. The programmer is a human being. It's generally very hard for a human to make a better decision than MSVC regarding whether to inline a function or not. Second, what is a programmer supposed to do when you tell him/her that a function should be inlined? The compiler is the one that is doing the inlining. The warning by itself does not tell you whether you have to do anything about it and, in if that was the case, what to do.
Many compilers still take the inline keyword into account in this case and use it to affect (read: increase) some kind of weighting that is used to decide whether or not said function would in fact be inlined.
This is true in MSVC. But the modern MSVC doesn't need this hint anymore. If it's beneficial to inline the function, the compiler will know and will inline it. If it's not, it will ignore the inline keyword which was inserted by a human.
Having the inline keyword there means that "warn if not inlined" warnings will be triggered (if enabled) if said functions are not inlined for whatever reason.
The MSVC compiler issues warning C4710 when a function that is selected for inlining was not inlined. This warning is disabled by default because most developers don't (and shouldn't) care about this warning. You can enable this warning, however. Unless you're a compiler optimizations researcher and want to understand the inlining algorithm used by MSVC, you shouldn't enable these warnings.