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?


  • The header file should compile on itself. ie. for testing make a .cpp file that just includes the header file.
  • The header file should be guarded by the pre-processor. if #ifndef etc...
  • Both these will ensure that the order will not matter.


    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.

    链接地址: http://www.djcxy.com/p/64196.html

    上一篇: C ++如何包含类头文件而不必编译它们的cpp文件?

    下一篇: 没有cpp文件的头文件,包括排序