Some books say that the inline keyword does not force a method/function to be inlined. It is a request. And some compilers automatically inline the code without the inline keyword(as judged by the compiler). If so, do not have to use the inline keyword?
Asked
Active
Viewed 63 times
1
-
2`inline` is a "hint" to the compiler, indicating that you'd like the function to be inlined. So, sure: if you would like the function in question to be inlined, go for it. It's not useless - though a good optimizer should, in theory, inline any functions that lend themselves to such treatment. Note that functions defined in header files are typically inlined by default (at the discretion of the compiler). – 3Dave May 23 '18 at 18:13
-
4@Dav3 *functions defined in header files are typically inlined by default (at the discretion of the compiler)* This is kind of misleading. Functions defined in header files should be explicitly declared `inline` by the user, so as to avoid multiple definition errors. – DeiDei May 23 '18 at 18:17
-
@Dav3, no. You are wrong on multiple occasions. First of, `inline` is not a hint to the compiler, modern compilers completely ignore `inline` for optimization purposes. Second, no, functions defined in header are not typically inlined. – SergeyA May 23 '18 at 18:31
-
@SergeyA Strictly speaking, `inline` *is* a hint to the compiler, though all modern compilers ignore it. – François Andrieux May 23 '18 at 18:32
-
@DeiDei not every function defined in the header must be declared inline. Templates and members defined in-class do not need to be declarted `inline`. – SergeyA May 23 '18 at 18:32
-
@FrançoisAndrieux I am not sure if you can call something a hint if the hint is not even considered. But I am not philosophy major. – SergeyA May 23 '18 at 18:33
-
Beyond having some real meaning in relation to ODR (the One Definition Rule), which has nothing to do with actual inlining, `inline` is nothing more than a hint/piece of advice to the compiler - which it is completely free to ignore. – Jesper Juhl May 23 '18 at 18:34
-
@SergeyA The language treats it as a non-binding request. Whether or not an implementation takes it into consideration does not change how the language is specified. – François Andrieux May 23 '18 at 18:34
-
@FrançoisAndrieux I think when it comes to optimizations, it's important to understand the actual behavior. I actually believe, standard should remove all wording regarding optimization aspect of `inline` and leave ODR aspect in it. This would be in line (pun intended) with modern compiler behavior. – SergeyA May 23 '18 at 18:37
-
1@SergeyA See the spec, *Since this meaning of the keyword inline is non-binding, compilers are free to use inline substitution for any function that's not marked inline, and are free to generate function calls to any function marked inline. Those optimization choices do not change the rules regarding multiple definitions and shared statics listed above. * (http://en.cppreference.com/w/cpp/language/inline) Not sure about you, but "non-binding", to me, means "hint". – 3Dave May 23 '18 at 18:42
-
And here's a nice discussion of how it's handled in GCC and clang. https://blog.tartanllama.xyz/inline-hints/ – 3Dave May 23 '18 at 18:53
-
You did not specify which version of C++ - note that in C++17, the `inline` keyword has new capabilities, and there it is _required_. For example, you can declare a non-integral static variable inside the header and initialize it right there: `inline static std::string s{"MyText"};` – Aganju May 24 '18 at 03:08