Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the inlining rules within C++ classes?

Tags:

c++

inline

From what I read somewhere long time ago, it seems that if you want class member function to be inlined during the compilation phase, the function has to be defined inside class declaration block.

But this has a downside of a detail leak. IMHO, other programmers should only see class interface when opening .h file.

Is the first statement still true in modern C++, was it ever? Is there a way to force inlining for functions that are declared, preferably in another file altogether?

Is it generally better to keep short member functions inside class declaration block, or not?

like image 265
Coder Avatar asked Jan 16 '23 12:01

Coder


2 Answers

It seems that if you want class member function to be inlined during the compilation phase, the function has to be defined inside class declaration block.

That is not really true. A function that is defined inside the class definition is implicitly marked as inline. But you don't need to defined the function inside the class for it to be inline, you can explicitly request it:

struct X {
   void f();
};
inline void f() {}

The inline keyword on the other hand, does not mean that the function will be inlined, but rather that it can be defined in multiple translation units, that is, if multiple translation units include the same header that contains that definition, the linker will not fail with a multiple definition error.

Now, on actual inlining, the compiler can decide to inline or not any function, regardless of whether the function is declared as inline provided that it sees the definition of that function (the code that it will inline), which is the reason why in general functions that are meant to be inlined should be defined in the header (either inside the class definition or marked inline outside.

Additionally, newer toolchains can perform whole program optimization or other link time optimizations, by which the linker can also decide that a function should be inlined. In this case, the function definition needs not be visible at the call site, so it could be defined inside the .cpp file. But if you really want the function to be inlined it is better not to depend on this feature and just define the function in the header.

like image 192
David Rodríguez - dribeas Avatar answered Jan 25 '23 08:01

David Rodríguez - dribeas


Q: Is there a way to force inlining for functions?

A: No

No matter how you designate a function as inline, it is a request that the compiler is allowed to ignore: it might inline-expand some, all, or none of the calls to an inline function.

Q: What are the inlining rules within C++ classes?

Inline member functions in C++

As far as Standard C++ is concerned, a inline function must be defined in every translation unit in which it is used ...

This is different from non-inline functions which must be defined only once in an entire program (one-definition-rule)...

For member-functions, if you define your function in the class, it is implicitly inline. And because it appears in the header, the rule that it has to be defined in every translation unit in which it is used is automatically satisfied.

Here is a great FAQ (one that's more "practical" than "pedantic"):

http://www.parashift.com/c++-faq-lite/inline-functions.html

like image 32
paulsm4 Avatar answered Jan 25 '23 08:01

paulsm4