Question

I'm now in the process of re-factoring c++ libraries I'm working on (mainly legacy), and I'm trying to minimize the include directives and recursive dependencies resulted from these inclusions. Also, for making the forward declaration handling neat and easy, I maintain a fwd.h in each library, (like iosfwd in std), which make library's clients life easier

Is there a case when include should be preferred over forward declaration (**when both cases compiles) ?**

I am not interested in school book explanation when forward declaration is preferable. Maybe there is a point when forward declaration is pain in the neck. What is the strategy in std, boost, or other well established libraries?

note: my compiler is intel icc 12 - supports only C++03 with very few C++11 features such as forward declaration of enums

ps: I have tackled with a similar discussion - and it appears that there is a case where forward declaration is worse than include - when deleting an incomplete an object of incomplete type: [C++ Class forward declaration drawbacks?

Was it helpful?

Solution 2

Only when the code won't compile! Always use and if possible refactor code to use forward declarations over #include's.

OTHER TIPS

#include is never better than forward declaration. Use #include when it is absolutely necessary. And #include is only necessary when the full type information is needed like declaring class member or invoking member method of in the containing class header file. In following cases it is not required:

  1. using pointer/reference as class member
  2. In function signature (parameter or return type)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top