Do I need .CPP files at all? Use only headers and make everything inline?

GCC 4.6.1 in particular.

I know that CPP files are used to separate the interface from the implementation; which is not of interest right now.

Looking at this , I see no reason not to use only headers and all the built-in functions.

Performance is troubling, but I do not see this approach slow down. What I don't want is to have critical sections that would normally be inline, to slow down because everything is inline. If that makes sense.

+4
source share
4 answers

Here are a few reasons not to:

  • No encapsulation; "Internal" methods are visible to the entire program.
  • Namespace pollution (resulting in naming conflicts or programmer error)
  • Addiction problems; it becomes much more difficult to ensure that everything is announced in the correct order.
  • Increase compilation time
+2
source

The main problem is compilation time, really.

If everything is included in a single master compilation unit, everything needs to be recompiled if you change one character in one file.

On the other hand, a complete rebuild will most likely be faster than if you used several compilation units (in this case, the same headers would need to be compiled several times, and the linker would have more work. With one compilation unit each the header needs to be processed only once, and the linker job is pretty trivial)

With multiple .cpp files, you can make changes to one of them and only recompile this file.

But several popular libraries only have a title. It is definitely viable.

In terms of performance, it should be the same or faster. You give the compiler full visibility throughout your code, which means that it can easily optimize function calls and embed whatever it likes.

And note that you never force the compiler to be embedded. The inline (and other tricks that have the same effect) do not tell the compiler that "this should be included." But, suppressing one definition rule (ODR), they allow you to include the definition in several compilation units, and therefore it becomes easier for the compiler to embed it if it wants to do this.

But that means you donโ€™t have to worry about everything being included. The compiler will only embed as much as it makes sense to do.

+3
source

Performance and understanding aside, there are things that you cannot do only with headings, for example, fields of a static class.

However, most (if not all) STL are just headers, like most Boost .

As for the built-in methods / functions - this does not really matter. The compiler knows better what to do, and can ignore the inline keywords (making the function not inline) or vice versa, make an inline function call, even if the function has not been declared as such.

+2
source

If all functions are "built-in", your binary will be larger, and this can lead to poor performance. You should include only very small and often called functions.

0
source

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


All Articles