This is more than just redundant; it is potentially problematic. Let's say Foo.h changes, so that Foo becomes typical of a particular instance of a universal, templatised equivalent - the kind of thing you would expect as part of a normal software evolution. Then Bar.cpp "class X" will be useless to cause ala compilation error:
--- fwd.h --- template <typename T> class XT { public: int n_; }; typedef XT<int> X; --- fwd.cc --- #include "fwd.h" class X; int main() { X x; x.n_ = 0; return x.n_; } --- compilation attempt --- ~/dev .../gcc/4.1.1/exec/bin/g++ fwd.cc -o fwd fwd.cc:3: error: using typedef-name 'X' after 'class' fwd.h:8: error: 'X' has a previous declaration here
This is one of the reasons I always recommend using the forward ala <iosfwd> highlighted declaration headers, which are supported and included in the main header to ensure consistent consistency. I never put "class X"; in the implementation file if the class is not defined in it. Remember that the apparent benefits are βclass Xβ; advanced declarations are not so much that they avoid #include , and moreover, the files that they include can be large and, in turn, include many other files: the highlighted direct declaration headers usually avoid the vast majority of them.
Tony delroy
source share