Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rules of thumb for putting functions in header files

Lately I have started to put more and more functions into header files, mostly for convenience. But I fear I might be overdoing it, my headers are full of includes and I'm not sure if that's a good idea.

What are your rules of thumb for moving functions out of or into header files?

In case you're wondering, I'm talking about developing applications, not libraries.

Edit:

I guess it's helpful if I outline the pros/cons of inline (naturally) header functions versus implementation functions from my point of view:

Pro inline:

  • More clean/concise.
  • No need for signature duplication.
  • No need to change any Makefile to link against new files.
  • Instant ability to introduce template parameters.

Contra inline:

  • Increased compile time (I don't care that much)
  • Many includes in headers (Shouldn't be such a big issue if they use guards)

According to that, it seems like a good idea to put pretty much every function in headers, and I believe that's pretty close to what the STL and Boost are doing (although those are libraries, as opposed to my code).

like image 726
futlib Avatar asked May 20 '11 06:05

futlib


1 Answers

One of my most inviolable rules: only function bodies which are inline are allowed in header files. Anything else is asking for trouble with multiple definitions in the link phase.

Headers should be left predominantly for declarations rather than definitions. I have exceptions to that rule (being the flexible type) but none of them involve non-inlined function bodies.

like image 94
paxdiablo Avatar answered Oct 18 '22 11:10

paxdiablo