为什么C ++中默认不包含卫兵? [英] Why aren't include guards in c++ the default?

查看:36
本文介绍了为什么C ++中默认不包含卫兵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的c ++项目的每个头文件中都使用了一次 #pragma (或者您使用了 #ifndef ... 的include Guards).这是巧合还是您在大多数开源项目中都能找到的(避免使用 only 依赖个人项目经验的答案).如果是这样,为什么不是这样:如果我想多次包含头文件,则使用一些特殊的预处理程序命令,否则,我将文件保持原样.

I use #pragma once (or you use include guards à la #ifndef...) basically in every header file in my c++ projects. Is this a coincidence or is it what you find for example in most open source projects (to avoid answers that rely only on personal project experience) . If so, why isn't it the other way around: If I want a header file to be included several times, I use some special pre-processor command and if not I leave the file as is.

推荐答案

C ++编译器的行为是根据其处理每个翻译单元的方式来指定的.预处理程序运行后,翻译单元是一个文件.我们习惯于在某些文件中收集声明并将其称为标头"文件,这一事实对编译器或C ++标准没有任何意义.

The behavior of a C++ compiler is specified in terms of how it handles each translation unit. A translation unit is a single file after the preprocessor runs over it. The fact that we have a convention of collecting declarations in certain files and calling them "header" files means nothing to a compiler or the C++ standard.

简而言之,该标准不提供头文件",因此它不提供自动包含保护头文件的条件.该标准仅提供预处理程序指令 #include ,其余仅是约定.没有什么可以阻止您向前声明所有内容并且不使用头文件(除了可惜的是,谁必须维护该代码...).

Simply put, the standard doesn't provide for "header files" so it can't provide for automatically include guarding header files. The standard only provides for the preprocessor directive #include and the rest is merely convention. Nothing stops you from forward declaring everything and not using header files (except pity for whomever should have to maintain that code...).

所以头文件不是特别的,也没有办法说保护头文件",但是为什么我们不能保护所有得到 #include 的东西呢?因为 #include 的功能比其他语言的模块系统功能强大或更低. #include 使预处理器粘贴到其他文件中,而不一定粘贴到头文件中.有时,如果在不同文件中的多个不同名称空间中使用相同的using和typedef声明,这可能会很方便.您可以将它们收集在文件中,并在几个地方 #include .您不希望自动包含防护措施阻止您这样做.

So header files aren't special and there's no way to say "that's a header file, guard it", but why can't we guard everything that gets #include'd? Because #include is both more and less powerful than a module system like other languages have. #include causes the preprocessor to paste in other files, not necessarily header files. Sometimes this can be handy if you have the same using and typedef declarations in a bunch of different namespaces in different files. You could collect them in a file and #include them in a few places. You wouldn't want automatic include guards preventing you from doing that.

使用 #ifndef #define 有条件地包含标头也只是惯例.该标准没有包括防护"的概念.(但是,现代编译器实际上已经意识到了包含保护.认识到包含保护可以允许更快地进行编译,但这与正确实现该标准无关.)

Using #ifndef and #define to conditionally include headers is also merely convention. The standard has no concept of "include guard". (Modern compilers however actually are aware of include guards. Recognizing include guards can allow faster compilation but it has nothing to do with correctly implementing the standard.)

上学了

该标准确实使用了标头"一词,特别是在引用C和C ++标准库时.但是, #include 的行为是在§16.2 *源文件*包含(例如我的Eminem)中定义的,它不赋予头文件任何特殊的权限.

The standard does liberally use the word "header", especially in reference to the C and C++ standard libraries. However, the behavior of #include is defined under § 16.2 *Source file* inclusion (emph. mine) and it does not grant any special powers to header files.

我们正在努力将适当的模块系统纳入C ++标准.

There are efforts to get a proper module system into the C++ standard.

这篇关于为什么C ++中默认不包含卫兵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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