tags:

views:

263

answers:

4

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?
+3  A: 

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).

sbi
Sorry, I didn't make myself clear. I am asking what's the reason for putting standard C89 functions into namespace std when they are then imported into global namespace. I have edited the question.
Alex B
As I said, TTBOMK, they are only in global namespace if you include `<header.h>`, not when you include `<cheader>`. But even if that would be wrong and they are injected into the global namespace by including `<cheader>`, my argument still holds: If, for example, another identifier `list` is (through some `using your_ns`) injected into the global namespace, you can still refer to them by their namespace name: `std::list`/`your_ns::list`. That's why it is a good thing they are in the `std` namespace.
sbi
+1  A: 

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 ;) ?

Matthieu M.
+1  A: 

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

MSalters
+3  A: 

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).

avakar
The wording of these paragraphs have been amended by C++0x, since especially the second is (imo) quite confusingly worded :) it reads now "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 placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3)."
Johannes Schaub - litb
I think it's worthy of inclusion into the actual answer.
Alex B