用于包括警卫队的名称? [英] Names used for include guards?

查看:86
本文介绍了用于包括警卫队的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人们在为自己的保镖选择名称时是否倾向于遵循任何准则?我不明白为什么 .h 文件的名称与包含保护中使用的名称略有不同.例如,我看过sphere.h,然后看过#ifndef SPHERE_H_.我可以很容易地使用SPHERE_还是名称必须匹配?下划线也是必需的吗?

解决方案

要为标头后卫选择好名字,请考虑以下几点:

1)名称必须是唯一的,因此请使其长而具体.通常可以通过包括项目名称,模块名称,文件名和文件扩展名之类的东西来实现.

2)确保您不使用为实现保留的名称.因此,以下划线开头,后跟一个大写字母的名称,包含双下划线的名称,以_t结尾的名称以及更多名称都将消失.

3)使名称对人类读者有意义.因此,不要只是生成一个UUID并使用它.

4)约定规定您将所有大写字母用于宏,以将其与普通变量区分开.

不需要 这样的下划线,但是由于不能在宏名称中使用空格,因此下划线是使内容保持可读性的一个很好的选择.

因此,恕我直言,项目baz内子模块栏中的文件foo.h的一个很好的保护名称是:BAZ_BAR_FOO_H.

Are there any guidelines people tend to follow when selecting a name for their include guard? I don't understand why the name for the .h file will slightly differ from the name used in the include guard. For example, I have seen sphere.h and then #ifndef SPHERE_H_. Could I just as easily have used SPHERE_ or do the names have to match? Are the underscores also necessary?

解决方案

To pick a good name for a header guard, consider these points:

1) the name has to be unique, so make it long and specific. That can usually be accomplished by including things like project name, module name, file name and file extension.

2) make sure you don't use names reserved for the implementation. So, names starting with underscore followed by a capital letter are out, as are names containing double underscore, names ending in _t and a few more.

3) make the name make sense to human readers. So don't just generate a UUID and use that.

4) convention dictates that you use all uppercase for macros to distinguish them from ordinary variables.

Underscores are not needed, as such, but since you cannot use spaces in macro names, underscores serve as a good alternative to keep things readable.

So, IMHO, a good guard name for a file foo.h in a submodule bar inside project baz is: BAZ_BAR_FOO_H .

这篇关于用于包括警卫队的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆