Injecting Inline Functions into .h Files and .cpp Files

What's the actual difference between this

//Foo.h
struct Foo {
  void bar() {
    //lots of complex statements
  }
};

      

and this one

//Foo.h
struct Foo {
  void bar();
};
//Foo.cpp
inline void Foo::bar() {
  //lots of complex statements
}

      

Are there any differences in the two approaches in the final compiled program, or is it guaranteed to be the same?

Please also make a few comments on which to choose and why in terms of good coding practice / experience. Note the "many complicated statements". Any specific cases where things like this should be in a header file? AFAIK, most of the header-only libs accelerators - why did they choose to do this?

+3


source to share


2 answers


Are there any differences in these two approaches in the final compiled program, or are they guaranteed to be the same?

Both are the same.
Member functions defined inside the body of a class / structure are implicit inline

.
However, in the first example, the function could be inline

d in every translation unit where this heading would be included. In the second example, the function can only be embedded in the cpp file that defines the function. This is because the compiler needs to see the definition of the function at the point of the function call in order to try to make it inline.

To answer Q to comments:

Assume that you save a function definition, marked as inline

in the cpp file A.cpp

, and try to call this function from another cpp file B.cpp

. As a result, you will get an "undefined external symbol" error, because in C and C ++ each translation unit is compiled separately and the definition of a function located in is A.cpp

not available at compilation B.cpp

.
Although if a function is only called from A.cpp

where it is defined, the definition is available in the same file, and the compiler and linker will happily compile and reference it.

C ++ 03 7.1.2 Function Specifiers :
Paragraph 3:

A function defined in a class definition is an inline function. The inline specifier must not appear in a block scope function declaration.




Which one to choose and why in terms of good coding practice / experience?

A class definition should serve as an interface for users of that class. Users of this interface do not need to see the implementation details of the functions. All they need to see is use this interface. By placing a function definition (rather long, as you mention) inside a class, you provide users with unnecessary data, thus making it difficult for them to see what they really need to see.
The best way to do it:

  • Declare a function in a class definition without a keyword inline

    and
  • Place the function definition with the keyword inline

    outside the class body.

Good read:
With inline member functions that are defined outside of the class, is it best to put the inline keyword next to the declaration inside the class body, next to the definition outside the class body, or both?




AFAIK, most of the header-only libs accelerators - why did they choose to do this?

Most Boost libraries are template libraries, and for template-based libraries, the compiler needs to see the definitions at the point of use, and therefore the template functions must be inlined.
Good Read:
Why Templates Can Only Be Implemented In A Header File?

+8


source


The two are equal in functionality. However, the latter is only possible for embedded in a file Foo.cpp

.



+2


source







All Articles