Home >Backend Development >C++ >Is Putting C Definitions in Header Files Truly the Universal Norm?

Is Putting C Definitions in Header Files Truly the Universal Norm?

Linda Hamilton
Linda HamiltonOriginal
2024-11-23 20:53:15936browse

Is Putting C   Definitions in Header Files Truly the Universal Norm?

Is Placing C Definitions in Header Files the Universal Norm?

One's programming style dictates that class declarations are stored in include files, while definitions reside in separate .cpp files. This approach aligns with the principle outlined in the widely regarded answer by Loki in the article "C Header Files, Code Separation."

However, a colleague insists that C declarations should generally encompass definitions within the same header file. He maintains that this is an universally adopted modern practice.

Verifying the Prevalence of This Practice

To ascertain the validity of this claim, let's examine the common practices observed in the industry.

Response

Your coworker's assertion is incorrect. The widespread practice has consistently been, and remains, to keep code in .cpp files (or other designated extensions) and declarations in headers.

While there are occasional benefits to placing code in headers, such as enhanced compiler inlining, it can also lead to prolonged compile times due to the code's repetitive processing upon inclusion. Additionally, it can introduce complexities with circular object relationships when code is contained within headers.

In summary, the prevailing practice of separating code and declarations aligns with your preference, contrary to your colleague's claim.

Exceptions and Clarifications

One noteworthy exception arises with templates. Modern libraries like boost heavily utilize templates and frequently adopt a "header only" approach. However, this is a unique scenario specifically applicable to templates.

Downsides of Header Only Code

It's important to consider the drawbacks of writing header only code:

  • Extended compile times, as evidenced by discussions on reducing compile times with Boost Asio.
  • Certain elements, such as global objects, cannot be implemented in header only libraries without resorting to singletons or other problematic solutions.
  • Incremental compilation benefits are lost, resulting in lengthy recompilation times for even minor changes.

Conclusion

The traditional practice of separating declarations and definitions remains the dominant approach in C . While there are specific scenarios where header only code may be beneficial, it's crucial to understand its limitations and use it judiciously. The claimed universal adoption of header only definitions is unfounded, and it's advisable to maintain the well-established practice of separating code and declarations.

The above is the detailed content of Is Putting C Definitions in Header Files Truly the Universal Norm?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn