Eric Auld
Eric Auld

Reputation: 1244

Implementing small functions in a header file, including in two different files in the same project

A quote from learncpp.com about whether to ever implement functions in header files:

  1. For classes used in only one file that aren’t generally reusable, define them directly in the single .cpp file they’re used in.
  2. For classes used in multiple files, or intended for general reuse, define them in a .h file that has the same name as the class.
  3. Trivial member functions (trivial constructors or destructors, access functions, etc…) can be defined inside the class.
  4. Non-trivial member functions should be defined in a .cpp file that has the same name as the class.

Suppose I follow the advice of #3, and I implement some small method my_class::f in the my_class.h file, but I leave the bigger methods to be implemented in my_class.cpp. If I include my_class.h in two different .cpp files in the same project, then the linker will see two (identical) definitions of my_class::f. (This is not solved by header guards.) I understand that some linkers are smart enough to tolerate this (though I've heard it can cost extra time). My question is:

Do you think they recommend #3 because it is uncommon to include the same .h file twice across a project, or because the linker can be relied upon to ignore the duplicate definition, and will not waste too much time doing so?

Upvotes: 1

Views: 1696

Answers (2)

eerorika
eerorika

Reputation: 238461

I understand that some linkers are smart enough to tolerate this

All standard conforming linkers tolerate multiple identical definitions of inline functions.

linker can be relied upon to ignore the duplicate definition

Yes.

and will not waste too much time doing so?

I would not expect the time wasted by the linker to be significant.

However, inline functions generally have to be compiled for each translation unit where they are used. If it is a particularly complex function, then the compiler (not linker) may end up wasting a significant amount of time. This is part of why the tutorial suggests defining only trivial functions inline.

Another reason to follow the (implied) advice (of not defining non-trivial functions inline) is that non-trivial functions attract changes, and modifications to header files propagate as changes to files that include those headers. Non-inline functions can cut re-compilation time to a tiny fraction of the full compilation time by virtue of not needing to be repeatedly compiled, and also not causing other translation units to be compiled when changed.

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311126

In the recommendation #3 there is written

Trivial member functions (trivial constructors or destructors, access functions, etc…) can be defined inside the class.

Member functions defined in a class are inline functions. So there is no problem with including the header in several compilation units.

Upvotes: 2

Related Questions