#include header guard format?

后端 未结 13 1608
广开言路
广开言路 2020-12-05 07:46

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

相关标签:
13条回答
  • 2020-12-05 08:19

    I prefer this format:

    #ifndef FOO_HPP
    #define FOO_HPP
    
    /* ... */
    
    #endif // FOO_HPP
    
    • A simple #ifndef instead of #if !defined(...), because it rarely makes sense to use a complex condition for a header guard.
    • The _HPP part to mark the identifier as a header guard.
    • No leading underscores, because such identifiers (starting with 2 underscores or with 1 underscore and capital letter) are reserved for the implementation.
    • The base part is just the file name, FOO. However, for library code that is going to be reused, it's advisable to add another part at the beginning. This is usually the containing namespace or the "module" name, e.g. MYLIB_FOO_HPP, and it helps to avoid naming conflicts.
    0 讨论(0)
  • 2020-12-05 08:20

    I use

    <FILENAME_IN_ALL_CAPS>_<YYYYMMDD>
    

    or

    <FILENAME_IN_ALL_CAPS>_INCLUDED_<YYYYMMDD>
    

    Keeping it synchronous with folder hierarchies is too annoying (friend of refactoring), GUIDs are too annoying, the date suffix is good enough. If I would have to equally named files on the same day, I would

    <FILENAME_IN_ALL_CAPS>_<YYYYMMDD>a
    <FILENAME_IN_ALL_CAPS>_<YYYYMMDD>b
    <FILENAME_IN_ALL_CAPS>_<YYYYMMDD>...
    
    0 讨论(0)
  • 2020-12-05 08:20

    I'd go with the filepath + the boost _INCLUDED suffix plus the nowadays widely supported #pragma once

    In alot editors (for me its sublime) you can also define some macros/snippets for this.

    Here is one that does it for you:

    <snippet>
        <content><![CDATA[
    #ifndef ${1:${TM_FILEPATH/(.*\/(include|src))*([^a-zA-Z0-9_]+)*([a-zA-Z0-9_]+)([.])*([a-zA-Z0-9_]+)*/\U$4_$6/ig}_INCLUDED}
    #define $1
    #pragma once
    
    
    $0
    
    
    #endif // $1
    ]]></content>
        <tabTrigger>incguard</tabTrigger>
        <description>include guard</description>
    </snippet>
    

    so yourproject/include/yourlib/yourfile.hpp

    becomes YOURLIB_YOURFILE_HPP_INCLUDED

    An additional external source code style checker tool could easily track consistency of your guards this way.

    0 讨论(0)
  • 2020-12-05 08:22

    I always included the namespace or relative path in the include guard, because only the header name alone has proven to be dangerous.

    For example, you have some large project with the two files somewhere in your code

    /myproject/module1/misc.h
    /myproject/module2/misc.h
    

    So if you use a consistent naming schema for your include guards you might end up with having _MISC_HPP__ defined in both files (very funny to find such errors).

    So I settled with

    MYPROJECT_MODULE1_MISC_H_
    MYPROJECT_MODULE2_MISC_H_
    

    These names are rather long, but compared with the pain of double definitions it is worth it.

    Another option, if you don't need compiler/platform independence you might look for some #pragma once stuff.

    0 讨论(0)
  • 2020-12-05 08:26

    I always use INCLUDED_FOO_HPP

    I wouldn't use the double underscore one, because starting things with double underscores is reserved.

    0 讨论(0)
  • 2020-12-05 08:26

    I always use use

    #ifndef FOOBAR_CPP
    
    0 讨论(0)
提交回复
热议问题