473,811 Members | 2,879 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing namespaces

Forgive me for asking so many questions so quickly. I am simply
asking as they come up unanswered in my book.

Can you change namespaces half way through a file? For instance, if
you want to use std and then half way through switch to your own:
{
...
using namespace std;
cout << "doing polygons";
using namespace polygon;
cout << poly1;
...
}

I know that this is a ridiculous example. Why would anyone define an
outstream for a 3D object with the name cout. But, suppose they did,
would this code snippet work, or do you have to use just one namespace
per file and be explicit about the use of other namespaces?
Jul 22 '05
21 1897
On 7 Jul 2004 13:34:39 -0700 in comp.lang.c++, bl*********@hot mail.com
(Blue Ocean) wrote,
{
...
{
using namespace std;
cout << "doing polygons";
}
{
using namespace polygon;
cout << poly1;
}
...
}

Jul 22 '05 #11
Russell Hanneken <me@privacy.net > wrote in message news:<Ck******* *********@newsr ead2.news.pas.e arthlink.net>.. .
Blue Ocean wrote:
[snip]

Since you're a Java programmer, this example might help:

import java.awt.*; // The java equivalent of a using directive.
import java.util.*;

public class Foo
{
public static void main (String[] args)
{
List list;
}
}

This Java code suffers from pretty much the same problem as your C++
example. Try to compile it and you'll see what I mean.


This was an extremely helpful example, and now my eyes are open.
Thank you very much for the help.

PS This is the same person that was posting under blue ocean; now that
I have a gmail account, google will only let me post under my gmail
address.
Jul 22 '05 #12
On Wed, 07 Jul 2004 21:56:28 GMT, Andre Kostur <nn******@kostu r.net> wrote:
JKop <NU**@NULL.NULL > wrote in news:p6******** *********@news. indigo.ie:
JustSomeGuy posted:
using namespace std;
using namespace system;
using namespace mine;


This will dump everything into the global namespace.

Not only can you just write:
string pp;
You can write:

::string pp;


Somebody correct me if I'm wrong, but this is bad info.

I think of the using namespace declarations as adding to a search path
for
identifiers, not actually copying them into other namespaces.

The "::string pp" is explicitly looking for a "string" identifier in the
global namespace, and no other namespaces. Thus the "::string" would not
be the same as "std::strin g".


I was surprised by JKop's example but I tried it on Comeau C++ and it
compiles. So unless Comeau is wrong the 'search path' model is wrong.
Should look at the standard I suppose.

john
Jul 22 '05 #13
On Wed, 07 Jul 2004 22:20:42 GMT, Russell Hanneken <me@privacy.net > wrote:
Andre Kostur wrote:
JKop <NU**@NULL.NULL > wrote in news:p6******** *********@news. indigo.ie:
JustSomeGuy posted:

using namespace std;

This will dump everything into the global namespace.

Not only can you just write:

string pp;

You can write:

::string pp;

Somebody correct me if I'm wrong, but this is bad info.


I think you're right. ::string fails to compile on GCC 3.3.3 and the
Comeau Online Compiler.


The following compiles for me on Comeau C++, MSVC++ 7.1 and gcc 3.3.1

#include <string>
using namespace std;

int main()
{
::string x = "";
}

What code did you try?

john
Jul 22 '05 #14
John Harrison wrote:
On Wed, 07 Jul 2004 22:20:42 GMT, Russell Hanneken <me@privacy.net > wrote:

I think you're right. ::string fails to compile on GCC 3.3.3 and the
Comeau Online Compiler.

The following compiles for me on Comeau C++, MSVC++ 7.1 and gcc 3.3.1

#include <string>
using namespace std;

int main()
{
::string x = "";
}

What code did you try?


Interesting. Testing with both GCC and the Comeau Online compiler, your
code works, and so does this:

#include <string>
using namespace std;
::string x = "";

However, this does not work:

#include <string>

int main()
{
using namespace std;
::string x = "";
}

I guess this is somehow related to the scope of the using directive?

--
Russell Hanneken
eu*******@cbobk .pbz
Use ROT13 to decode my email address.
Jul 22 '05 #15
On Thu, 08 Jul 2004 05:32:45 GMT, Russell Hanneken <me@privacy.net > wrote:
John Harrison wrote:
On Wed, 07 Jul 2004 22:20:42 GMT, Russell Hanneken <me@privacy.net >
wrote:

I think you're right. ::string fails to compile on GCC 3.3.3 and the
Comeau Online Compiler.

The following compiles for me on Comeau C++, MSVC++ 7.1 and gcc 3.3.1
#include <string>
using namespace std;
int main()
{
::string x = "";
}
What code did you try?


Interesting. Testing with both GCC and the Comeau Online compiler, your
code works, and so does this:

#include <string>
using namespace std;
::string x = "";

However, this does not work:

#include <string>

int main()
{
using namespace std;
::string x = "";
}

I guess this is somehow related to the scope of the using directive?


I think I'm really going to have to look this up in the standard, which is
always a scary prospect. But here's a guess, your version of the code
injects all the std namespace names into the scope of main, but ::string
ignores any names within the scope of main and just looks directly at the
global scope. I could be utterly wrong.

I don't like what I'm learning here though. I prefer the search path
model, but maybe there is some subtle reason why it wouldn't work.

john
Jul 22 '05 #16
Blue Ocean wrote:
Forgive me for asking so many questions so quickly. I am simply
asking as they come up unanswered in my book.

Can you change namespaces half way through a file? For instance, if
you want to use std and then half way through switch to your own:
{
...
using namespace std;
cout << "doing polygons";
using namespace polygon;
cout << poly1;
...
}

I know that this is a ridiculous example. Why would anyone define an
outstream for a 3D object with the name cout. But, suppose they did,
would this code snippet work, or do you have to use just one namespace
per file and be explicit about the use of other namespaces?

You should try to use using std:: statements in case you use 2-3 std
facilities, and use using namespace statements for more. And both these,
*in an as small scope as possible*.

Do not use using statements in global scope.

For example:
#include <iostream>
int main()
{
using std::cout;
using std::cin;

int x;

cin>>x;

// ...

cout<<x<<"\n And then...\n";

// ...

cout<<"End of operations!\n";
}


Another example:

#include <iostream>
#include <vector>
int main()
{
using namespace std;

vector<int>arra y(10);

for(vector<int> ::size_type i=0; i<array.size() ; ++i)
cin>>array[i];

// ...

}

Never use using statements in global scope. Doing this, is an attempt to
defeat the namespace system.
In this way you can define your own versions of facilities in your own
namespaces and use them interchangeably .


Regards,

Ioannis Vranos
Jul 22 '05 #17
John Harrison wrote:
I think I'm really going to have to look this up in the standard, which
is always a scary prospect. But here's a guess, your version of the
code injects all the std namespace names into the scope of main, but
::string ignores any names within the scope of main and just looks
directly at the global scope. I could be utterly wrong.

I don't like what I'm learning here though. I prefer the search path
model, but maybe there is some subtle reason why it wouldn't work.



From TC++PL 3:
"Operator summary:
scope resolution class_name :: member
scope resolution namespace_name :: member
global :: name
global :: qualified-name
"


Regards,

Ioannis Vranos
Jul 22 '05 #18

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:opsas0q1jt 212331@andronic us...
On Thu, 08 Jul 2004 05:32:45 GMT, Russell Hanneken <me@privacy.net > wrote:
John Harrison wrote:
On Wed, 07 Jul 2004 22:20:42 GMT, Russell Hanneken <me@privacy.net >
[snip]
I think I'm really going to have to look this up in the standard, which is
always a scary prospect. But here's a guess, your version of the code
injects all the std namespace names into the scope of main, but ::string
ignores any names within the scope of main and just looks directly at the
global scope. I could be utterly wrong.


I think you are right.
Section 3.4.3/4
A name prefixed by the unary scope operator :: (5.1) is looked up in global
scope, in the translation unit where it is used. The name shall be declared
in global namespace scope or shall be a name whose declaration is visible in
global scope because of a using-directive (3.4.3.2). The use of :: allows a
global name to be referred to even if its identifier has been hidden
(3.3.7).

-Sharad
Jul 22 '05 #19
"Sharad Kala" <no************ ******@yahoo.co m> wrote in
news:2l******** ****@uni-berlin.de:

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:opsas0q1jt 212331@andronic us...
On Thu, 08 Jul 2004 05:32:45 GMT, Russell Hanneken <me@privacy.net >
wrote:
> John Harrison wrote:
>> On Wed, 07 Jul 2004 22:20:42 GMT, Russell Hanneken
>> <me@privacy.net >

[snip]

I think I'm really going to have to look this up in the standard,
which is always a scary prospect. But here's a guess, your version of
the code injects all the std namespace names into the scope of main,
but ::string ignores any names within the scope of main and just
looks directly at the global scope. I could be utterly wrong.


I think you are right.
Section 3.4.3/4
A name prefixed by the unary scope operator :: (5.1) is looked up in
global scope, in the translation unit where it is used. The name shall
be declared in global namespace scope or shall be a name whose
declaration is visible in global scope because of a using-directive
(3.4.3.2). The use of :: allows a global name to be referred to even
if its identifier has been hidden (3.3.7).


I stand corrected. Personally it seems kinda weird, but thems the
breaks. I wonder why the decision was made that using the "::" scope
operation still goes through the "search path"... I would have figured
that by saying "::" you _meant_ global only...
Jul 22 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
3055
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of a single collections type, I should be proposing a new "namespaces" module instead. Some of my reasons: (1) Namespace is feeling less and less like a collection to me. Even though it's still intended as a data-only structure, the use cases...
11
1797
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I understand that you declare your intention to use a namespace within a page through the "Inherits" attribute. I know that using "Inherits" isn't absolutely necessary, it's just recommended so the developer doesn't have to type out the entire...
0
9722
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
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10644
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...
0
9200
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...
0
5550
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...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4334
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
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
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.