473,772 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1922
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::f oo()
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
3516
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 using std::cin; using std::cout;
6
1741
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
3590
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 amount of preprocessor directives used in these code. Can anyone please tell me how to actually use these preprocessor directives in a more professional way like selecting particular lines of code suited for some particular hardware etc...
5
5511
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: #if TEST
13
2790
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 files?
30
4122
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" << std::endl; Myself I am not sure which I prefer, it is certainly easier to specify that the std namespace is being used instead of tagging each member of the namespace?
7
2228
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
1641
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 System; using System.Collections;
6
5254
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 { ... } namespace part2 { ... } }
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9911
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.