Re: Rob Pike's simple Include rule

From: Thomas Matthews (Thomas_MatthewsSpitsOnSpamBots_at_sbcglobal.net)
Date: 04/23/04


Date: Fri, 23 Apr 2004 16:19:41 GMT

Neil Cerutti wrote:

> In Rob Pike's style guide he urges the following:
>
> Simple rule: include files should never include include
> files. If instead they state (in comments or implicitly)
> what files they need to have included first, the problem of
> deciding which files to include is pushed to the user
> (programmer) but in a way that's easy to handle and that,
> by construction, avoids multiple inclusions.
>
> I was startled by this guideline, since it's not what I think of
> as the usual practice, i.e., brute-force idempotency through
> header guards in the headers.

In our shop we have the types
   UINT8, UINT16, UINT32
defined a unsigned integers with the given bitwidth.

We have one file, let's say types.h, which defines those types
for various platforms.

So if I write a function:
   UINT8 Public_Function(UINT16 variable)
   {
    /* ... */
   }

and I want to make it public, I put it into a header file
my_functions.h:
   UINT8 Public_Function(UINT16 variable);

Now, since it uses the UINT8 and UINT16 in the declaration,
those identifiers must be resolved before this function
declaration.

According to the style guide, a programmer must include
the types.h file before the my_functions.h file.

If the my_functions.h file has the statements:
     #ifndef UINT8
     #include "types.h"
     #endif
before the function declaration, the user only has to
worry about including one function. The header file
takes care of its own declaration issues.

-- 
Thomas Matthews
C++ newsgroup welcome message:
          http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq:   http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
          http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
     http://www.josuttis.com  -- C++ STL Library book


Relevant Pages

  • [PATCH -next] k8.h: add struct bootnode
    ... k8.h uses struct bootnode but does not #include a header file ... so provide a simple declaration for it. ... Please read the FAQ at http://www.tux.org/lkml/ ...
    (Linux-Kernel)
  • [PATCH 2/4] [NET] make non-global function __dev_addr_discard static
    ... and remove its declaration in header file include/linux/netdevice.h ... More majordomo info at http://vger.kernel.org/majordomo-info.html ... Please read the FAQ at http://www.tux.org/lkml/ ...
    (Linux-Kernel)
  • Re: static variables and dll/lib troubles
    ... it does not belong in the header file! ... In my dll project, I link to myLib.lib to build a myDll.dll. ... myDll.cpp includes myLib.h to access the static int* p. ... The first step is to immediately remove the declaration from the header file. ...
    (microsoft.public.vc.mfc)
  • Re: Error C2259 while building application on 64-bit
    ... build to the header file you use in the 64-bit build. ... 'HRESULT IPersistFolder::Initialize': is ... declaration of 'IShellFolder::ParseDisplayName' ... while compiling class template member function 'HRESULT ...
    (microsoft.public.vc.mfc)
  • Re: External structs - newbie question
    ... The header file for csd.c contains the typedef for a ... There is only one struct in use ... Declaring it in a header file is the right way to go. ... If you put the keyword extern before the declaration, it will not be a problem declaring it twice. ...
    (comp.lang.c)