All the name in a C header that are available via the cXXX equivalent (e.g. cstdlib) have the names put in the std namespace.
But, you don't have to put std:: in front for compatibility reasons.
This compiles (at least on Visual Studio 2005):
1 2 3 4 5 6 7 8
#include <cstdlib>
int main()
{
system("pause");
return 0;
}
EDIT: so it is in the standard namespace (when doing #include <cstdlib> ):
(C++ 2003)
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).
So that means #include <stdlib.h> behaves like:
1 2 3 4
#include <cstdlib>
using std::system;
//do the same for the remainder of the symbols in cstdlib
So, does the program I posted earlier not comply with the standard?
Don't know the big deal about it. Looking back at all my books that say to use it point out it is only good for making the console window to stay open. They all also went on to show the one 'quick' method I use.
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
int main()
{
char ch;
std::cout << "Press any key and ENTER to continue..." << std::endl;
std::cin >> ch;
return 0;
}
Not the best, but when I am doing console apps that is all I need to see the output. System is normally used just to pause it for viewing the output since most compilers just flash it up and close it. I seldom seen it used outside that purpose to be honest.
#include <cstdlib>
int main()
{
system("pause");
return 0;
}
This is standard C++. That doesn't mean the resulting program will actually work, or do what the programmer intended. Syntactic validity and semantic validity are entirely different things.
Some users try to use -pedantic to check programs for strict ISO C conformance. They soon find that it does not do quite what they want: it finds some non-ISO practices, but not all---only those for which ISO C requires a diagnostic, and some others for which diagnostics have been added.
A feature to report any failure to conform to ISO C might be useful in some instances, but would require considerable additional work and would be quite different from -pedantic. We don't have plans to support such a feature in the near future.
As long as std::system compiles it is following the standard. However it pollutes the global namespace...
Anyway, I was just curious if the statement I posted earlier, "[C library functions] are only kind of in the std namespace", carried anything more than anecdotal weight to it.
It seems that if you #include <cstdlib> , for example, the C library functions are in the std namespace as well as the global namespace.
The new standard adds a caveat in its description of this subject (Emphasis mine on the final sentence):
(C++11 Draft N3242)
(17.6.1.2.4): Except as noted in Clauses 18 through 30 and Annex D, the contents of each header cname shall be the same as that of the corresponding header name.h, as specified in the C standard library (1.2) or the C Unicode TR, as appropriate, as if by inclusion. In the C++ standard library, however, the declarations (except for names which are defined as macros in C) are within namespace scope (3.3.6) of the namespace std. It is unspecified whether these names are first declared within the global namespace scope and are then injected into namespace std by explicit using-declarations (7.3.3).
So, we have unspecified behaviour here: (1) either the declarations of the C library are in namespace std (and that's it), or (2) the declarations are in the global namespace and are then injected into namespace std.