BEST WAY TO LEARN C++!!!

Pages: 12
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?
Last edited on
Don't know. Everything I found says it is std::system but when I checked both compiled fine without any using calls.
closed account (1vRz3TCk)
So, does the program I posted earlier not comply with the standard?

From my understanding of the standard, the following is not compliant
1
2
3
4
5
6
7
8
#include <cstdlib>

int main()
{
    system("pause");

    return 0;
}

However it always seems to compile. :0)
1
2
3
4
5
6
7
#include <cstdlib>

int main()
{
    system("clear");
    return 0;
}
albatross@quantum:~$ g++ -Wall -pedantic ./Documents/testpad.cpp
albatross@quantum:~$


I suppose that would mean it's standards-compliant, unless even with -pedantic g++ doesn't follow the standard as strictly as I thought it does.

-Albatross
Last edited on
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.
Last edited on by closed account z6A9GNh0
Guys what is is this std:: stuff, I mean what is the difference between:

cout << "Hello World!";

and

std::cout << "Hello World!";

Just wondering :D
closed account (zwA4jE8b)
std is a namespace

if you
using namespace std;
you will not have to type
std::
in front of functions/commands in the std namespace

http://cplusplus.com/doc/tutorial/namespaces/
Last edited on
1
2
3
4
5
6
7
8
#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.
Last edited on
@CreativeMFS

Ah ok thankyou very much man, much appreciated.
@Albatross,
Try -pedantic -ansi -std=c++0x
@chrisname
albatross@quantum:~$ g++ -Wall -pedantic -ansi -std=c++0x ./Documents/testpad.cpp
albatross@quantum:~$


testpad.cpp didn't change between tests. My g++'s version is 4.5.2.

-Albatross
man gcc wrote:
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...
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.
Pages: 12