C++ #ifndef for include files, why is all caps used for the header file?

后端 未结 7 1837
猫巷女王i
猫巷女王i 2021-01-06 12:32

I wonder why the name after the #ifndef directive is always all caps and don\'t seem to match the name of the actual header file? What are the rules surrounding

7条回答
  •  清歌不尽
    2021-01-06 12:50

    The idea is to make sure your header file is only read once during build. The idiom to accomplish that is the structure:

       #ifndef _SOME_UNIQUE_NAME
       #define _SOME_UNIQUE_NAME
       /* The actual header code */
       #endif
    

    This means that you should choose a name that you are pretty sure will be unique and is a valid identifier for #ifndef. You should also make sure that the identifier is not used in actual code or confused with a variable or something. Having an upper case tag marks the idiom clearly. Besides that, it is merely conventions not language that dictate that choice. Visual Studio's wizards generates a GUID like identifier for. Sone compilers support #pragma once that have the same effect.

提交回复
热议问题