C Header Files: Definition Placement
Your coworker's assertion that all C declarations and definitions should be placed in header files is unfounded. The common practice in C , historically and currently, remains to separate code definitions into .cpp files and declarations in header files.
Reasons for Definition Separation
Exception: Templates
The notable exception to this rule is templates. Modern C libraries like Boost make extensive use of templates, which require definition in headers to allow for inlining optimizations. However, this practice should be limited to templates only.
Downsides of Header-Only Code
While header-only code can be convenient for templates, it has several drawbacks:
Conclusion
Despite your coworker's claims, the separation of definitions into .cpp files and declarations in header files remains the preferred approach in C for maintaining code organization, optimizing compile times, and avoiding potential pitfalls.
The above is the detailed content of Should C Definitions Always Be Placed in Header Files?. For more information, please follow other related articles on the PHP Chinese website!