473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

A program of the using directives for the nested namespace.

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
3 1893
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: beliavsky | last post by:
Many of my C++ programs have the line using namespace std; but the "Accelerated C++" book of Koenig and Moo has many examples where the library names are included one at a time, for example ...
6
by: Baloff | last post by:
Hello I wrote a code which is suppose to read a file which contains lines of double and prints it out. thanks for helping double.txt*************************************** 1.01 2.0301
13
by: seemanta dutta | last post by:
Greetings C gurus, I have used preprocessor directives since a very long time. But whenever I see some professional piece of C code, the linux kernel for example, I get literally confused by the...
5
by: cody | last post by:
the following leads to an error (note that TEST is not defined): #if TEST string s = @" #"; // <-- the error is "preprocessing directive expected" #endif also, here we get the same error: ...
13
by: rincewind | last post by:
I remember reading an article (was it Herb Sutter's?) that recommended avoiding using directives. While I quite understand this recommendation for headers, what's wrong in using directive in .cpp...
30
by: Pep | last post by:
Is it best to include the code "using namespace std;" in the source or should each keyword in the std namespace be qualified by the namespace tag, such as std::cout << "using std namespace" <<...
7
by: patrick | last post by:
Why do some code listings for learning C++ have the entire namespace std being used while others just specify the parts they want to use?
7
by: =?Utf-8?B?QmVu?= | last post by:
Hi, I sometimes see code where the 'using' statements at the top of the file are located before the namespace declaration, and sometimes they are located inside it. For example: using...
6
by: Daniel Kraft | last post by:
Hi all, in my C++ projects, I usually make "heavy use" of namespaces to "encapsulate" my code. When I have for instance something like this: namespace project { namespace part1 { ... }...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.