Difference between revisions of "Cpp Coding Standards/HEADERS"

From Apache OpenOffice Wiki
Jump to: navigation, search
(some improvements)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
Topic-Id: '''HEADERS'''
+
== Header Files (HEADERS) ==
 +
''What to do or do not with header files.''
  
What to do or do not with header files.
+
===== Self Sufficient and Minimal <span id="Self">(Self)</span> =====
----
+
Header files should be self sufficient and minimal. [[/Self|-> Details]]
=== Summary ===
+
  
==== Self Sufficient and Minimal <span id="Self">(Self)</span> ====
+
===== Include Directly <span id="IncDirect">(IncDirect)</span> =====
 +
Include the header files for all types you need directly, not via another file. [[/IncDirect|-> Details]]
  
When included a header file should be self sufficient and minimal.
+
===== Precompiled headers <span id="IncPCH">(IncPCH)</span> =====
* Include definitions for superclasses, data members, typedefs, macros, types denoted in exception-specifications, ...
+
The first statement in each cxx file has to be the inclusion of the precompiled header of the module. [[/IncPCH|-> Details]]
* Create forward declarations for everything else (but note that this introduces unwanted coupling, when for example a template declaration is later on extended with defaulted parameters, or a struct definition is replaced with a typedef)
+
* Include the definition as late as possible, this helps reduce physical dependencies
+
  
// Foo.hxx
+
===== Internal Include Guards <span id="IncGuards">(IncGuards)</span> =====
// Superclass and data member
+
Use internal include guards, but don't use the external ones. [[/IncGuards|-> Details]]
#include "Super.hxx"
+
#include "Data.hxx"
+
+
// Forward declarations
+
class Param;
+
+
// This does not need a class definition
+
Param doSomething1(Param aParam);
+
+
class Foo : public Super
+
{
+
  public:
+
  // This does not need a class definition either
+
  Param doSomething(Param aParam);
+
 
+
  private:
+
  Data mData;
+
};
+
  
// Anotherfile.cxx
+
===== No Entities with Linkage (NoLinkage) =====
#include "Foo.hxx"
+
Don't define entities with linkage in header files.<br>
#include "Param.hxx" // Now we need the definition
+
Exception: Constants.
+
[[/NoLinkage|-> Details]]
...
+
  Param p;
+
  p = doSomething1(p); // Error without the definition
+
  
 
+
===== No using namespace (NoUsingNsp) =====
==== Include Directly <span id="IncDirect">(IncDirect)</span> ====
+
Don't write "using <namespacename>" in heaer files. [[/NoUsingNsp|-> Details]]
Include the header files for all types you need directly, not via another file.
+
 
+
==== Precompiled headers <span id="IncPCH">(IncPCH)</span> ====
+
 
+
* The first statement in each cxx files should be the inclusion of precompiled headers. There must be no other includes, no definitions and no include guards around the include statement. Forget this for a new file and you break the build.
+
* Assume the precompiled header file is empty and include all your headers normally.
+
 
+
#include "precompiled_foo.hxx" // May or may not contain includes for Foo.hxx and Bar.hxx
+
+
#include "Foo.hxx"
+
#include "Bar.hxx"
+
 
+
==== Internal Include Guards <span id="IncGuards">(IncGuards)</span> ====
+
Internal include guards are good but '''don't''' use the external ones. They add a lot of noise for a non-existent benefit.
+
 
+
// Foo.hxx
+
+
// Yes, use internal include guards
+
#ifndef INCLUDED_FOO_HXX
+
#define INCLUDED_FOO_HXX
+
+
// No include guards here
+
#include "Superclass.hxx"
+
...
+
+
#endif
+
 
+
// Bar.cxx
+
+
// No include guards here
+
#include "Bar.hxx"
+
#include "Foo.hxx"
+
#include "Baz.hxx"
+
  
 
----
 
----
 
[[Category:Coding Standards]]
 
[[Category:Coding Standards]]

Latest revision as of 09:22, 23 May 2007

Header Files (HEADERS)

What to do or do not with header files.

Self Sufficient and Minimal (Self)

Header files should be self sufficient and minimal. -> Details

Include Directly (IncDirect)

Include the header files for all types you need directly, not via another file. -> Details

Precompiled headers (IncPCH)

The first statement in each cxx file has to be the inclusion of the precompiled header of the module. -> Details

Internal Include Guards (IncGuards)

Use internal include guards, but don't use the external ones. -> Details

No Entities with Linkage (NoLinkage)

Don't define entities with linkage in header files.
Exception: Constants. -> Details

No using namespace (NoUsingNsp)

Don't write "using <namespacename>" in heaer files. -> Details


Personal tools