我应该包括< xxxx.h>或< cxxxx>在C ++程序中? [英] Should I include <xxxx.h> or <cxxxx> in C++ programs?

查看:132
本文介绍了我应该包括< xxxx.h>或< cxxxx>在C ++程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 我应该在C ++程序中包含什么, stdio.h cstdio ?为什么?

  • 为什么两个头文件提供相同的功能?

  • 标准对此有何评价?

  • 我应该如何处理其他此类标题,是否有我应该遵循的基本规则?


解决方案

请考虑以下计划:



  #include< cstdio> 

使用std :: printf;

int main()
{
printf(Hello World);
返回0;
}

注意指令 using namespace std; ,尤其是在标题中,不是一个好的选择,你应该总是使用使用声明。



请注意,我们考虑 stdio.h cstdio 这里只是一个示例用例,实际上它适用于 all most cxxxx xxxx.h 标题,除了一些像< math.h> < cmath>


  • What should I include in C++ programs, stdio.h or cstdio? and Why?
  • Why two header files which provide the same functionality?
  • What does the standard say regarding this?
  • How should I go about including other such headers, Is there a base rule that I should follow?

解决方案

Consider the following programs:

Sample 1:

#include<stdio.h>

int main()
{
    printf("Hello World");
    return 0;
}

Sample 2:

#include<cstdio>

int main()
{
    printf("Hello World");
    return 0;
}

Both work as expected. So which usage is more appropriate? The answer is: Neither! Surprised? Read on.

The C++ Standard library provides all standard C headers for compatibility reason, while C++ as a language also provides all the equivalent headers. As a convention,

  • No C++ standard library headers(apart from ones include for C compatibility) have any file extensions, and
  • All C++ equivalent of C headers begin with cxxxxx.

The C++ Standard mentions this under Annex D (normative) Compatibility features:

§2 mentions the important distinguishing point. This rule applied to the examples above means:

  • Including cstdio imports the symbol names in the std namespace and possibly in the Global namespace.
  • Including stdio.h imports the symbol names in the Global namespace and possibly in the std namespace.

Let us apply this rule to our sample codes and measure the pros and cons:

Sample 1: This brings all the symbols from stdio.h in the global namespace. Advantage is that you can use the symbols without any qualification since they are imported in the global namespace. Downside is that you end up polluting the global namespace with many symbol names that you will probably never use. This might lead to symbol name collision. In C++ always consider the global namespace as a minefield and avoid it as much as possible.

Sample 2: This is a very bad practice because there is no guarantee that the implementation will put the symbols in global namespace, the standard simply does not demand to do so. We are simply relying on the behavior of one particular compiler implementation. We cannot and should not assume that all compilers will do so. So strictly speaking the program is not standard approved and this usage is not portable across all implementations.

So what is the correct usage?

The correct usage is to use cstdio and fully qualify the symbol names or else bring them in scope with using declarations. This guarantees all symbols we use are present in std namespace and we are not polluting the global namespace. Example of correct usage:

Sample 3:

#include<cstdio>

using std::printf;

int main()
{
    printf("Hello World");
    return 0;
}

Note that the directive using namespace std;, especially in a header, is not a good option and you should always use using declarations.

Note that we consider stdio.h vs. cstdio here just a sample use case, in practice it applies to all most cxxxx and xxxx.h headers, except a few like <math.h> and <cmath>.

这篇关于我应该包括&lt; xxxx.h&gt;或&lt; cxxxx&gt;在C ++程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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