Difference between revisions of "Cpp Coding Standards/HEADERS"

From Apache OpenOffice Wiki
Jump to: navigation, search
(Internal Include Guards <span id="IncGuards">(IncGuards)</span>)
 
(5 intermediate revisions by 4 users 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 and data members
+
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
+
* 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 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 _FOO_HXX
+
#define _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