Search code examples
c++headerinclude

Is there a downside to including a cpp file over a header file?


I understand that header files have their uses, especially in the case of interfaces, but is there any reason you shouldn't include a cpp file directly? You can still use include guards and/or pragma once inside a cpp so is there really any reason besides maybe going against standard practice?


Solution

  • We have header files for a reason. It (mostly) separates the interface of some code from its implementation. Some of the benefits include the compiler having to go through less code and not having to recompile every file that uses a function when you change its definition. Including source files can also violate ODR as pointed out in the comments.