a newbie's question about namespace

Z

ZF Tang

As you know,if I include "cstdio, cstdlib,...", instead "stdio.h,
stdlib.h,...",
their content will be declared in the std namespace.

But the following code is compiled successfully even though "using namespace
std" is commented out, why?

#include <cstdio>
using namespace std;
void main()
{
printf("Hello, world!\n");
}
 
R

Rolf Magnus

ZF said:
As you know,if I include "cstdio, cstdlib,...", instead "stdio.h,
stdlib.h,...",
their content will be declared in the std namespace.

But the following code is compiled successfully even though "using
namespace std" is commented out, why?

Probably for the same reason it accepts void as return type of main. It is
faulty.
 
A

Alf P. Steinbach

* Rolf Magnus:
Probably for the same reason it accepts void as return type of main. It is
faulty.

Well, no.

It's just impractical for a compiler vendor to make the C header files
include the C++ header files and put everything also in the global
namespace: then separate C and C++ version of the same header files
would have to be maintained (impractical), or some heavy trickery used.

Instead what's done is to let the C++ header files include the C ones
and place the global namespace declarations also in namespace 'std';
when the standard specifies 99.999% impractical things, compiler vendors
simply do not comply -- the case here, and the case for 'export'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,979
Members
47,580
Latest member
kim2namjoon

Latest Threads

Top