#include header guard format?

后端 未结 13 1624
广开言路
广开言路 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条回答
  •  Happy的楠姐
    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.

提交回复
热议问题