I know it makes little difference to a project but, assuming you use #defined header guards for your C++ code, what format do you use? e.g. assuming a header called f
I tend to use:
#ifndef FILE_DATE_H_
(replace _H_ with the appropriate extension like _HPP_, etc). The date stamp is to avoid collisions with other same named headers in other directions/libraries.
so in the end it looks like this:
#ifndef SOMEFILE_20082411_H_
I use
#if !defined(FOO_HPP_INCLUDED)
I prefer the modern defined
syntax because it allows || && operators, even if they aren't used here.
Also
#ifndef __FOO_HPP__
is technically illegal, as leading underscores are reserved.
Personally, i just use the filename FOO_HPP. Google uses the whole path like SRC_ENGINE_FAST_HPP.
Certain sets of names and function signatures are always reserved to the implementation:
- Each name that contains a double underscore (_ _) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the implementation for any use.
- Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.
(17.4.3.1.2/1
)
When I'm being paid for my time, and there isn't already a company standard, I use:
#ifndef path_to_file_h
#define path_to_file_h
The reason for the lowercase is that it's easier to copy and paste filenames and replace slashes with underscores. The reason for #ifndef is that it lines up nicely with #define, making it easier to see that the symbols are the same. I like the GUID idea, though, so I might try it out.
When I'm not being paid for my time, and not releasing my code into the wild, I just use #pragma once
. Unlike most other portability issues, it's just as easy to add include guards later as now, and it can be done by someone who knows nothing about the code base (e.g. me in a year's time, or some innocent programmer I send my code to), so YAGNI applies.
If you are using Visual Studio or a Microsoft compiler use the pragma way
#pragma once
To truly avoid name collisions, I use GUIDs:
#ifndef GUARD_8D419A5B_4AC2_4C34_B16E_2E5199F262ED