5

I've seen code that uses forward headers that are #include'd where otherwise there would be forward declarations. By "forward header" I mean a header that includes only forward declarations. So you might have class.cpp, class.h, and class_fwd.h or just class.h and class_fwd.h if class.h contains templates.

Examples of this are in Boost, which has somefile_fwd.hpp files.

When is creating and including a forward header preferable to explicit forward declarations? When is it not? What are the advantages and disadvantages of this practice? Should one create a forward header for all headers?

This question is essentially the same:

Forward declaration include, on top of declaration include (ClassFwd.h + Class.h)

Why I don't find the answers in that question satisfactory:

  • Daniel Lidström's answer describes something related that he does but does not address the question specifically.
  • Sebastian's answer mostly explains why forward declarations are used in general and then comments on a pattern similar to what Daniel Lidström describes.
  • Dialecticus's answer is vague but does hint at templates being a reason to use forward headers.
  • Cheers and hth. - Alf's answer is similar to Dialecticus's answer.
Community
  • 1
  • 1
Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
  • Well, if a library provides lots of types (like ``), and it is reasonable for users to only need forward declarations in their headers (e.g. `std::ostream & foo(std::ostream &)`), then it is only polite for the library to provide a forward-declaration-only header. – Kerrek SB Jun 15 '14 at 02:00
  • Explanation? Is this referring the dup that I linked? Didn't I address that? – Praxeolitic Jun 15 '14 at 02:01
  • 2
    If there is a question that is essentially the same, but you want a substantially better answer, one way to get it is to offer a small bounty to attract attention. Add a comment why you find the current answers unsatisfactory. Disclaimer: I don't think I can give a better answer than what's already there. – Sergey Kalinichenko Jun 15 '14 at 02:06

1 Answers1

2

If the forward declarations are kept in a header, then the forward header can be included in the main header and/or implementation files so that the compiler can check for One Definition Rule violations. (e.g. mismatches in number of template arguments)

This is essentially the same reason that headers with function prototypes should be visible when the function is actually defined.

Also, the guideline "Don't Repeat Yourself" advises against duplicating the same declaration many times. Not because duplication wastes disk space, or even because of the difficulty of propagating changes to multiple locations (although this is often cited), but to prevent the possibility of incompatible versions from existing at all.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Just to make sure, you're talking about forward headers that aggregate forward declarations (especially for a whole lib) but there's absolutely no reason to create forward headers to match every individual header, right? – Praxeolitic Jun 15 '14 at 04:11
  • These benefits apply equally to split or aggregated forward headers. I would aggregate depending on how closely related things are. – Ben Voigt Jun 15 '14 at 04:34
  • Just one last thing - hat exactly does "main header" mean? Is that a header created as a single include for using a lib? – Praxeolitic Jun 20 '14 at 21:39
  • @Praxeolitic: What I mean is that header that contains the full class definition. – Ben Voigt Jun 20 '14 at 21:58