Naming Include Guards

2019-01-07 23:29发布

How are C++ include guards typically named? I tend to see this a lot:

#ifndef FOO_H
#define FOO_H

// ...

#endif

However, I don't think that's very intuitive. Without seeing the file name it's difficult to tell what FOO_H is there for and what its name refers to.

What's considered best practice?

9条回答
我只想做你的唯一
2楼-- · 2019-01-08 00:15

Replace FOO_H with FOO_H_INCLUDED and it's clearer.

查看更多
时光不老,我们不散
3楼-- · 2019-01-08 00:16

From my own experience, the convention is to name the inclusion guards after the header file containing them with the exception that the name is all in caps and the period is replaced with an underscore.

So test.h becomes TEST_H.

Real life examples of this include Qt Creator, which follows this convention when auto-generating class header files.

查看更多
女痞
4楼-- · 2019-01-08 00:16

I usually look what time it is and just append that to the end of it, i.e. FOO_H_248, it's an extra precaution, and you'll never have to remember it anyway, so you don't need to worry about the fact that it's cryptic.

查看更多
登录 后发表回答