Kai
Kai

Reputation: 2832

Headers without cpp files and include ordering

There was a highly rated response in a question about header ordering with the following suggestion:

Good practice: every .h file should have a .cpp that includes that .h first before anything else. This proves that any .h file can be put first.

Even if the header requires no implementation, you make a .cpp that just includes that .h file and nothing else.

Personally I've never had a problem with include ordering for headers that don't have a corresponding cpp file. What kinds of problems does this best practice prevent?

Upvotes: 0

Views: 1556

Answers (2)

Tod
Tod

Reputation: 8232

One problem it solves is allowing the .h file to be linted (at least by my lint tools). Without a .cpp doing an include of an .h my template code gets skipped.

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 59987

  1. The header file should compile on itself. ie. for testing make a .cpp file that just includes the header file.
  2. The header file should be guarded by the pre-processor. if #ifndef etc...

Both these will ensure that the order will not matter.

Upvotes: 3

Related Questions