简体   繁体   中英

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

This is less of an "I'm having this issue" question, and more of an "I really want to understand how the language works better" question.

I've recently started encountering definitions of inline functionality in .cpp files for a given class. I'd like to understand what the difference between the definition points for inline functionality actually is. In doing an object level analysis of inlines defined in the CPP, it seems like a much higher percentage of the inlines declared in the C++ (as opposed to being defined in the .h) are optimized to mapped functions instead of being legitimately inlined - is this the major difference, or is there some other purpose behind this entirely which I'm not seeing.

There is no difference. inline is a hint to the compiler, but isn't particularly important these days, since compilers are pretty good at figuring out whether to expand functions inline without your help (see register keyword).

inline also tells the compiler that multiple definitions in different translation units are okay (provided they're the same), which is needed when you put an inline function in a header file.

When an inline function is defined in a .cpp file its definition is only visible in that file, so calling it from some other source file won't work.

inline functions don't make sense without one and only one single definition per translation unit , so it makes sense to place it in a header file, where the definition may be reused. When an inline function is only used in one source file, it makes sense to define it locally. It's all a matter of context.

It's likely that the difference is a result of your compiler not doing "link-time"/"whole-program" optimization. This is when the compiler performs optimizations while looking at the whole program instead of just a single translation unit. It's often not turned on by default even in compilers that support it because it typically involves very high memory usage.

When only optimizing on the translation-unit level, it's not possible to inline functions that are defined in other source files because the definition is not available.

An inline function is easier for the compiler to inline if it is defined before the call rather than after. Since header files are typically included at the top of the source, this condition is more easily met.

Technically, there is no difference.

Although I don't know much about optimizations with them, think of it...

Preprocessor will first expand the .h file where it finds #include in .cpp file. And then it will be presented to the compiler. So technically there is no difference at all.

But, there is a rule:

An inline function must be defined in every compilation unit in which it is called. (Of course, ODR must be followed).

This is because, every compilation unit is handled by a separate instance of the compiler program.

Hence, usually, inline function are defined in the header file which is included in every .cpp file in which there is a call to that function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM