A program of the using directives for the nested namespace.

W

Wayne Shu

When I read the chapter of the namespace of the book C++ Primer(3e).
It explain the using directive as follow:
"A using directive makes the namespace member names visible as if they
were declared outside the namespace at the location where the namespace
definition is located."

I have some doubt about the using directives for the nested namespace,
so I wrote a program to test it,
#include <iostream>

namespace test
{
namespace nested
{
void foo() {}
}
}

using namespace test::nested;

int main()
{
foo()
return 0;
}

I think this program will wrong when compile it. But it is correct.
Why??
I think the statement "using namespace test::nested;" make the
namespace like this
namespace test
{
void foo() {};
}
and the function foo can't be seen in the main() function.
But this program can compile without error. How does it happen??

Thanks for anwsering it, and forgiving my poor English.
 
V

Victor Bazarov

Wayne said:
When I read the chapter of the namespace of the book C++ Primer(3e).
It explain the using directive as follow:
"A using directive makes the namespace member names visible as if they
were declared outside the namespace at the location where the
namespace definition is located."

I have some doubt about the using directives for the nested namespace,
so I wrote a program to test it,
#include <iostream>

Why do you need to include said:
namespace test
{
namespace nested
{
void foo() {}
}
}

using namespace test::nested;

int main()
{
foo()

There is a missing semicolon here.
return 0;
}

I think this program will wrong when compile it. But it is correct.

No, it is not.

If you fix the semicolon after "foo()", then it is correct because
the name "foo" has been introduced into the global namespace by
your 'using' directive.
I think the statement "using namespace test::nested;" make the
namespace like this
namespace test
{
void foo() {};

Drop the semicolon, it's a syntax error.

No, it does not make the namespace like this. It would make it like
this if you placed your 'using' directive _inside_ the 'test' namespace.
and the function foo can't be seen in the main() function.
But this program can compile without error. How does it happen??

By putting the 'using' directive in the global namespace you put _all_
symbols declared in 'test::nested' into the *global namespace*.

V
 
V

VJ

Wayne said:
#include <iostream>

namespace test
{
namespace nested
{
void foo() {}
}
}

using namespace test::nested;

int main()
{
foo()
return 0;
}


it is same as this:


#include <iostream>

namespace test
{
namespace nested
{
void foo() {}
}
}

int main()
{
test::nested::foo()
return 0;
}
 
W

Wayne Shu

Oh, I got it, thanks.

Victor said:
There is a missing semicolon here.


No, it is not.


If you fix the semicolon after "foo()", then it is correct because
the name "foo" has been introduced into the global namespace by
your 'using' directive.


Drop the semicolon, it's a syntax error.


No, it does not make the namespace like this. It would make it like
this if you placed your 'using' directive _inside_ the 'test' namespace.


By putting the 'using' directive in the global namespace you put _all_
symbols declared in 'test::nested' into the *global namespace*.

V
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top