question on namespace

M

Milan

Is there any difference between:

a) using namespace std;
namespace MyNamespace
{
: // my own code
}

b) namespace MyNamespace
{
using namespace std;
: // my own code
}


I found some of my code do not compile if (b) is used. Why?

Regards,
Milan.
 
V

Victor Bazarov

Milan said:
Is there any difference between:

a) using namespace std;
namespace MyNamespace
{
: // my own code
}

b) namespace MyNamespace
{
using namespace std;
: // my own code
}

Yes, there is. The latter case brings symbols defined in
'std' namespace into 'MyNamespace'. The former brings them
into the namespace that contains 'MyNamespace', probably the
global one.
I found some of my code do not compile if (b) is used. Why?

Hard to say without seeing your code.

Victor
 
F

Fogus

b) namespace MyNamespace
{
using namespace std;
: // my own code
}


I found some of my code do not compile if (b) is used. Why?


Because the using directive for std is restrained within the MyNamespace
scope. If you try to access any std entities outside of MyNamespace
as if they were global (i.e. as if you had used a using std at the
top-level scope), then they will fail because the cannot 'see' inside.

-m
 

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
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top