I ran into a situation where my build speeds have started to become large and have affected productivity. I had already minimized header dependencies before using forward declarations. Now I've turned to forward declarations to further reduce these dependencies as a method of trying to reduce compile times.
I understand the concept of forward declarations, but I didn't quite understand how much you could forward declare until I came across this. As I understand it, I can forward declare all method/function parameter and return types for function declarations.
I have a lot of situations where I have (assume I have include guards in header):
//myclass.h
#include "someclass.h"
#include "someotherclass.h"
class MyClass{
public:
SomeOtherClass foo(SomeClass y);
};
With forward declarations, I can now do something like:
//myclass.h
class SomeClass;
class SomeOtherClass;
class MyClass{
public:
SomeOtherClass foo(SomeClass y);
};
//include actual header files in myclass.cpp
I believe this will require the header to be included at the call site some how, but removes the include from the header. And the caller doesn't use that call, I would assume It would just be a net gain of needing that dependency at all on the call site.
I haven't seen this pattern before (only with class members and private function types), so I'm wondering if there is any downside to doing this. Is there a reason why I shouldn't just forward declare all of these types?