By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,636 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

A program of the using directives for the nested namespace.

P: n/a
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.

Oct 24 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Wayne Shu wrote:
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 <iostream>?
>
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.
Why??
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 24 '06 #2

P: n/a
VJ
Wayne Shu wrote:
#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;
}
Oct 24 '06 #3

P: n/a
Oh, I got it, thanks.

Victor Bazarov wrote:
Wayne Shu wrote:
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 <iostream>?

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.
Why??

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 25 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.