0

I read that it is really bad to include things in header files (for compilation speed). I often place the definitions of my functions templates in a MyClass.hxx file that I #include "MyClass.hxx" from "MyClass.h". Since MyClass.hxx needs all of my includes, and they are by definition included directly in the .h - that seems very bad. Is there any way to avoid this?

David Doria
  • 9,873
  • 17
  • 85
  • 147
  • 4
    There is absolutely nothing wrong with a header file including other headers. – James McNellis Dec 13 '11 at 17:09
  • Isn't this the whole idea of the PIMPL idiom? – David Doria Dec 13 '11 at 17:10
  • So longs as your header gaurds are in check this isn't a problem, if you are including absolutely loads of stuff to the point where compilation is painful then you might want to restructure the code into smaller units. – 111111 Dec 13 '11 at 17:15
  • @DavidDoria, not exactly. PIMPL is more useful in other situations, just to fight with recompiling source files that use frequently changing headers (well, it's not the best description, anyway...). – Griwes Dec 13 '11 at 17:34
  • pImpl is most useful when you want to use things from platform-specific headers or quarrelsome headers (e.g., windows.h) but you don't want the rest of your project to be forced to include those headers, so that platform-dependent code is encapsulated. It's occasionally useful to improve compilation speed, but really, compiler tricks like precompiled headers are far more useful for this. – James McNellis Dec 13 '11 at 17:52
  • @DavidDoria Unrelated note: I think ".hxx" files are a mediocre idea at best. The important thing for these kinds of logistics is that someone can immediately navigate and understand your code. Most people don't segregate template definitions like that, so it won't be intuitive. – David Dec 13 '11 at 19:02

1 Answers1

0

I asked a question relevant to this one a while back: pimpl for a templated class

Your other option is precompiled headers.

Unless you are running into unacceptable compile times I would just use good general practices to reduce compile times like forward declaring in the headers and #including in the cpps (when possible) and only #including what it necessary. The end result doesn't change even if you #include everything under the sun in every file, the unused stuff will get stripped.

Community
  • 1
  • 1
David
  • 27,652
  • 18
  • 89
  • 138