Thumb rules for placing functions in header files

Recently, I started adding more and more functions to header files, mainly for convenience. But I'm afraid I might overdo it, my headlines are full of inclusions, and I'm not sure if this is a good idea.

What are your rules of thumb for moving functions from or to header files?

In case you are interested, I’m talking about application development, not libraries.

Edit:

I think this is useful if I outlined the advantages / disadvantages of the built-in (naturally) header functions and implementation functions from my point of view:

Pro inline:

  • Cleaner / concise.
  • No need to duplicate a signature.
  • No need to change the Makefile to link to new files.
  • Instant ability to enter template parameters.

Contra inline:

  • Increased compilation time (I don't care)
  • Many include in headlines (shouldn't be such a big problem if they use guards)

Accordingly, it seems like a good idea to give almost all the functions in the headers, and I find it pretty close to what STL and Boost do (although they are libraries, unlike my code).

+7
source share
7 answers

One of the most irreconcilable rules: only function bodies that are built-in are allowed in header files. Everything else poses problems with multiple definitions in the link phase.

Headings should be left primarily for declarations and not for definitions. I have exceptions to this rule (which is a flexible type), but none of them include non-built-in function bodies.

+5
source

My rule is: "Not in the title unless you need to." And for convenience, do you find the increase in compilation time convenient?

+3
source

There are several obvious technical aspects - templates and built-in functions should be in the headers - headers included from several translation units should be careful with the Unified Definition Rule - more frankly, you need a bloody good reason to even think about including an out-of-competition function in the header , and I can’t think that they didn’t even seduce me.

So, the question boils down to the following:

embedded in header or off line in implementation file

Factors:

  • You say that you are developing application-level code, not libraries, so you don’t have to (at present) worry that other commands depend on your code, nor do they minimize their recompilation (compared to just relink), keeping line implementation
    • BUT, if you write good code that could potentially be useful for other teams, you might want to keep private
  • inline versus out-of-line usually represents an order of magnitude overhead for trivial data get / set functions ... if you have functions that are repeatedly called from critical performance code, then you have reason to prefer embedding
  • the implementation in the header (especially if it is mixed with declarations) can often confuse the API, but sometimes it actually makes the code more self-documenting
  • and remote redundancy (union of declarations / definitions) certainly eliminate the potential for typos / errors and can often improve performance

Bottom line: if you find yourself doing it more and more, then this obviously works for you, and there is no reason to think that you are going to burn out. Watch for potential problems, but do not overdo it without thinking about what is happening on the basis of some kind of hypothetical and unlikely to materialize the problem.

+2
source

A good coding standard will tell you to implement methods and functions in the source file (cpp).

If you prefer this, you can implement templates and inline functions in the header.

0
source

Since this has been marked as C++ , why don't you split them into logical class es?

Usually I have one class declaration in the header file and its definition in the corresponding source file.

0
source

Two rules that I use:

1) If it is built-in functions

2) If it is a template function.

0
source

First, the template function should be placed in the headers.

In addition, functions with an empty body, such as the default constructor or the default, but a virtual destructor, can be placed in headers.

I never use inline because the compiler does not guarantee this.

0
source

Source: https://habr.com/ru/post/888617/


All Articles