What are the rules for embedding in C ++ classes?

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

But this has the disadvantage of detailing the leak. IMHO, other programmers should only see the class interface when opening the .h file.

Is the first statement still relevant in modern C ++, has it ever been? Is there a way to force the declared functions, preferably in another file?

As a rule, is it better to store short member functions inside the class declaration block, or not?

+4
source share
3 answers

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

This is not true. A function defined inside a class definition is implicitly marked as inline . But you do not need to define the function inside the class so that it is inline , you can explicitly request it:

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

On the other hand, the inline does not mean that the function will be built-in, but rather can be defined in several translation units, i.e. if multiple translation units include the same header that contains this definition, the linker does not fail with a multiple definition error.

Now, with the actual embedding, the compiler can solve the built-in or not some function, regardless of whether the function is declared as inline , provided that it sees the definition of this function (the code that it will be embedded), which is the reason that the general functions that need to be built in must be defined in the header (either inside the class definition, or marked inline externally.

In addition, new tools can perform whole-program optimizations or other link-time optimizations, with which the linker can also decide what function should be built-in. In this case, the definition of the function should not be visible on the call site, so it can be defined inside the .cpp file. But if you really want the function to be built-in, it is better not to depend on this function and just define the function in the header.

+6
source

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

A: no

Regardless of how you designate the function as inline, this is a request that the compiler is allowed to ignore: it can inline-expand some, all, or none of the calls to the inline function.

Q: What are the rules for embedding in C ++ classes?

Inline member functions in C ++

As for the C ++ standard, it is necessary to define a built-in function in each translation unit in which it is used ...

This is different from non-built-in functions that need to be defined only once in the entire program (one definition rule) ...

For member functions, if you define your function in a class, it is implicitly inline. And since it appears in the title, the rule that it must be defined in each translation unit in which it is used is automatically executed.

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

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

+2
source

Is the first statement still relevant in modern C ++, has it ever been?

As David explained, there is an inline keyword. It can be ignored, as Paul said.

Is there a way to force embedding for declared functions, preferably in another file in general?

Perhaps setting up your compiler. In any case, it may occupy you behind your back. For instance. gcc has -finline-functions etc. that will be enabled for certain optimization levels

As a rule, is it better to store short member functions inside the class declaration block or not?

To you. Keep in mind that if you use the built-in method many times, then you can increase the size of your object files and therefore potentially inflate the size of what you are building and possibly slow it down.

FWIW I'm only trying to put implementations in header files from laziness :)

0
source

Source: https://habr.com/ru/post/1415776/


All Articles