c++ - Does defining a function inside a header always make the compiler treat it as inline? -


I have now learned that defining the C ++ function inside the class header file makes the function inline. But I know that putting inline keywords next to a function is just a suggestion and the compiler does not consider it necessary. Is this header defined for the c ++ function and is there a difference in the behavior between a standalone C ++ function and a C ++ function which is a part of the class?

"defines a C ++ function inside the class header file inline functions"

It is not true defining a function inside a definition of a class (which is to say, by providing only the function's body instead of just one declaration) inline. "It makes it inline", I mean it is similar to giving inline keywords, but class definitions are not in the header, and headers can include other things from classroom definitions.

In this example, the function foo is interlinked inline. Function bar is not inline:

  struct foo {void foo () {} void bar (); }; Zero Fu :: Bar () {}  

"There is only one suggestion by putting inline keywords next to a function and the compiler does not consider it necessary"

Inline has two effects, one of them is a sign of the compiler, which can ignore it. The second is not optional, and it always has an effect "hint" is that the compiler is advised to change the call in that function with a copy of the code for the function

The guaranteed effect is that An inline function can be defined in many translation units, and they are linked together without many definition errors, and all the same copies are removed by the linker. Therefore, if the above example header appears in the file that is shared between several translation units, then bar must be clearly marked inline otherwise, the linker bar which are not allowed.

In spite of the name, inline is mostly about the other in C ++, the compulsory effect, not the first, the optional one modern optimizing compilers have their own ideas, whose The call should be inline, and do not pay too much attention to inline while completing that decision. For example, I have seen that it impacts the moderate optimization level in GCC, but at a lower level Almost anything Is not included, and at the higher level there is almost everything (if the definition is available, when the call is compiled) unless it does not function is too big.

Whether the function is defined in a header or in the CPP file, there is no effect on anything by it. You can safely imagine that copy #include and pastes the header file to the preprocessor in the CPP file, before the compiler sees it if a function is defined as a call in the same translation unit , The function code is to be inline by the compiler. If they are in different translation units, the code is not available and the call can only be linked with full program optimization or similar by the linker. A "translation unit" has more or less meaning, "CPP file has copied and pasted all headers".


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -