C ++ - What is the difference between the strings defined in CPP and H

This is less the question "I have this problem," and another question, "I really want to understand how the language works better."

Recently, I began to encounter definitions of built-in functions in .cpp files for this class. I would like to understand what is the difference between the definition points of inline functionality. When analyzing the level of objects inline strings defined in CPP, it seems that a much larger percentage of inline strings declared in C ++ (as opposed to the definition in .h) is optimized for associated functions, and is not legally inline - this is the main difference , or is there some other goal that I don’t see completely.

+4
source share
5 answers

There is no difference. inline is a hint to the compiler, but these days it’s not particularly important, because compilers are pretty good at whether to expand functions inside a string without your help (see the register keyword).

inline also tells the compiler that several definitions in different translation units are consistent (provided that they are the same), which is necessary when installing the built-in function in the header file.

When a built-in function is defined in a .cpp file, its definition is visible only in this file, so a call from some other source file will not work.

+3
source

inline functions do not make sense without one and only one definition for a translation unit, so it makes sense to place it in the header file, where the definition can be reused. When the inline function is used in only one source file, it makes sense to define it locally. All this in context.

+2
source

Probably the difference is that your compiler does not perform link-time / whole-program optimizations. This is when the compiler performs optimization when viewing the entire program, and not just one translation unit. It often does not turn on by default even in compilers that support it, because it usually uses very high memory.

When only optimization is at the level of a translation unit, it is impossible to embed functions that are defined in other source files, because the definition is not available.

+1
source

An inline function is easier for the compiler to inline if it is defined before the call, and not after. Since header files are usually included at the beginning of the source, this condition is easier to fulfill.

+1
source

Technically, there is no difference.

Although I know little about optimization with them, think about it ...

The preprocessor will first expand the .h file, where it will find #include in the .cpp file. And then it will be presented to the compiler. So technically there is no difference.

But there is a rule:

The an inline function must be defined in each compilation unit in which it is called. (Of course, ODR should be respected).

This is because each compilation unit is processed by a separate instance of the compiler program.

Therefore, usually an inline function is defined in the header file, which is included in each .cpp file in which there is a call to this function.

0
source

All Articles