ANSI C functions namespace in ISO C++ [duplicate]

那年仲夏 提交于 2019-12-11 11:45:34

问题


Consider the following small program:

#include <cstdio>

int main() {
    printf("%d\n", 1);
    std::printf("%d\n", 2);
    return 0;
}
  1. What does C++ standard say about importing C library functions into global namespace by default? Can you point me to the relevant C++ standard section?
  2. What is the reason ANSI C functions are in std namespace in the first place, since they are by default imported into global namespace?

回答1:


7.4.1.2/4:

Except as noted in clauses 18 through 27, the contents of each header cname shall be the same as that of the corresponding header name.h, as specified in ISO/IEC 9899:1990 Programming Languages C (Clause 7), or ISO/IEC:1990 Programming Languages—C AMENDMENT 1: C Integrity, (Clause 7), as appropriate, as if by inclusion. In the C + + Standard Library, however, the declarations and definitions (except for names which are defined as macros in C) are within namespace scope (3.3.5) of the namespace std.

D.5/2:

Every C header, each of which has a name of the form name.h, behaves as if each name placed in the Standard library namespace by the corresponding cname header is also placed within the namespace scope of the namespace std and is followed by an explicit using-declaration (7.3.3).

In practice, however, name.h includes the names in the global namespace and cname in both the global and std namespace (i.e. the other way around than the standard specifies).




回答2:


As to your first question: TTBOMK, unqualified printf shouldn't work in standard C++ when including <cstdio>.

To the second question: For one, the C++ standard library uses an incredible amount of identifiers, among the such useful and common ones like list, sort, iterator and the like. Had they all been in the global namespace, we'd been effectively robbed of hundreds of useful names.

Also, if you use the explicit std:: prefix for a few weeks, you will probably find it easier to read the code, since it very effectively tells you which identifiers are from the standard library (so you know them right away).




回答3:


Personally I like namespaces, especially short ones.

People usually think that since you have a namespace to type it is longer, but that's wrong. One of the benefit of typing the namespace is that your editor can then use auto-completion much more efficiently ;)

Besides typing (since one could argue that you type faster than auto-completion anyway), there is also the net advantage that if I type 'std::re' I will be prompted with all the identifiers that begin with 're' in the std namespace, saving me the hassle of memorizing each and every one of them, and then I can also look at their arguments directly, in case I don't remember the exact order, the const-ness, etc...

Technically, there is also something about clashes of names... but who care ;) ?




回答4:


<stdio.h> is the header which imports the functions from <cstdio> into the global namespace.



来源:https://stackoverflow.com/questions/1524139/ansi-c-functions-namespace-in-iso-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!