Add forward declarations in some headers#1460
Add forward declarations in some headers#1460Explorer09 wants to merge 1 commit intohtop-dev:mainfrom
Conversation
|
IMO we should keep forward declarations to a minimum. As htop already uses IWYU for minimizing includes, it's mostly a matter of keeping those recursive dependencies at bay. Apart from some core types, there should – in the best case – be no further such loops. Regarding this concrete PR: Not a fan of it at all … Personally I find that repeated Also, the code style (although IIRC not explicitly written) encourages to scope includes of headers to be as minimal as possible. If you need a type only for an implementation, only include it with the |
|
Is there anyone else wanting to comment on this issue? I am considering closing this PR. |
I'm not sure this code quality issue is worth fixing in htop, but I hope getting some discussions.
In #1454 I mentioned a "header dependency hell" that motivated me to split the
MeterModeIdtype into a separate header. It is this one.When I added an
#include "Meter.h"line intoSettings.h, a circular inclusion of headers would happen and that results in build errors in many.cfiles.Meter.hincludesMachine.hwhich includesSettings.hwhich would then includeMeter.h, but due to the header guard, the latterMeter.hinclude line resolves to no-op. Sometimes a module might include justMachine.hor justSettings.hbut the circular inclusion can happen either case.When a C header depends on a
typedefof another type, it has to include the header defining that type as the type redefinition (typedefdefining the same type twice) is an error in C. Forward declarations only work withstruct,union,enumandclass(C++) names, but nottypedefs, but htops usestypedefs extensively even forstructs in the coding style.When a structure like
Meteronly include a reference of another class such asMachine, there's no need to fully define theMachinetype nor include theMachine.hin order to use it (in the header; the implementation part, i.e.Meter.cwould have to includeMachine.hof course). A forward declaration could work equally well for a header.The commit I presented is not a complete fix. It's a part of header changes that, when using forward declarations, can fix build errors I encountered. Is it a good idea to apply such forward declarations whenever possible in htop headers?