473,406 Members | 2,293 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,406 software developers and data experts.

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 #1
21 1846
On 7 Jul 2004 13:34:39 -0700, Blue Ocean <bl*********@hotmail.com> 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 aren't changing namespaces, you are adding to the set of namespaces
that will be searched. You can have as many 'using namespace ...' as you
like and all of them will be searched.

In your example, if cout was defined in namespace std and polygon, then
you would get a compiler error 'ambiguous name cout' or some such.

john
Jul 22 '05 #2
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 think you misunderstand using directives. A using directive makes
names from a namespace available as if they had been declared in the
global namespace. The second using directive in your example doesn't
nullify the effect of the first one. Assuming there is a polygon::cout,
the code would fail to compile, because there would be a name conflict.

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.

--
Russell Hanneken
eu*******@cbobk.pbz
Use ROT13 to decode my email address.
Jul 22 '05 #3
John Harrison wrote:
On 7 Jul 2004 13:34:39 -0700, Blue Ocean <bl*********@hotmail.com> 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 aren't changing namespaces, you are adding to the set of namespaces
that will be searched. You can have as many 'using namespace ...' as you
like and all of them will be searched.

In your example, if cout was defined in namespace std and polygon, then
you would get a compiler error 'ambiguous name cout' or some such.

john


Wow.. So if you put:

using namespace std;
using namespace system;
using namespace mine;

at the top of your code then all three namespaces will be searched... and
assuming there is no conflict they will be resolved?

So if you have "std:ios:in" in your code, you could change it for
using namespace std:
using namespace ios:
and then just specify "in"?

Jul 22 '05 #4
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;
Ahh!!
-JKop
Jul 22 '05 #5
JustSomeGuy wrote:

Wow.. So if you put:

using namespace std;
using namespace system;
using namespace mine;

at the top of your code then all three namespaces will be searched... and
assuming there is no conflict they will be resolved?
Sure.
So if you have "std:ios:in" in your code, you could change it for
using namespace std:
using namespace ios:
and then just specify "in"?


I think you meant to type semicolons rather than colons at the end of
your using directives. Anyway, that wouldn't work, because ios is a
class, not a namespace.

--
Russell Hanneken
eu*******@cbobk.pbz
Use ROT13 to decode my email address.
Jul 22 '05 #6
On Wed, 07 Jul 2004 15:34:37 -0600, JustSomeGuy <No***@ucalgary.ca> wrote:
John Harrison wrote:
On 7 Jul 2004 13:34:39 -0700, Blue Ocean <bl*********@hotmail.com>
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 aren't changing namespaces, you are adding to the set of namespaces
that will be searched. You can have as many 'using namespace ...' as you
like and all of them will be searched.

In your example, if cout was defined in namespace std and polygon, then
you would get a compiler error 'ambiguous name cout' or some such.

john


Wow.. So if you put:

using namespace std;
using namespace system;
using namespace mine;

at the top of your code then all three namespaces will be searched... and
assuming there is no conflict they will be resolved?


Right

So if you have "std:ios:in" in your code, you could change it for
using namespace std:
using namespace ios:
and then just specify "in"?


No because ios is not a namespace, its a typedef for a template. But yes,
in general you're right.

namespace outer
{
namespace inner
{
int xxx;
}
}

using namespace outer;
using namespace inner;

int main()
{
xxx = 1;
}

john
Jul 22 '05 #7
On Wed, 07 Jul 2004 21:41:41 GMT, JKop <NU**@NULL.NULL> wrote:
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;
Ahh!!


Interesting, but ugly. I suppose that demonstrates that it's not really
the case that namespaces are searched, but rather that the names from a
namespace are put into the global namespace.

john

Jul 22 '05 #8
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::string".
Jul 22 '05 #9
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.

--
Russell Hanneken
eu*******@cbobk.pbz
Use ROT13 to decode my email address.
Jul 22 '05 #10
On 7 Jul 2004 13:34:39 -0700 in comp.lang.c++, bl*********@hotmail.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****************@newsread2.news.pas.earthl ink.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******@kostur.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::string".


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>array(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:opsas0q1jt212331@andronicus...
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.com> wrote in
news:2l************@uni-berlin.de:

"John Harrison" <jo*************@hotmail.com> wrote in message
news:opsas0q1jt212331@andronicus...
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

"Andre Kostur" <nn******@kostur.net> wrote in message
news:Xn*******************************@207.35.177. 134...
"Sharad Kala" <no******************@yahoo.com> wrote in
news:2l************@uni-berlin.de:

"John Harrison" <jo*************@hotmail.com> wrote in message
news:opsas0q1jt212331@andronicus...
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...


Isn't the point is that there isn't a search path? Once you use 'using
namespace xxx;' within the global namespace all the names in xxx *are*
global.

john
Jul 22 '05 #21
"John Harrison" <jo*************@hotmail.com> wrote in
news:2l************@uni-berlin.de:

"Andre Kostur" <nn******@kostur.net> wrote in message
news:Xn*******************************@207.35.177. 134...
"Sharad Kala" <no******************@yahoo.com> wrote in
news:2l************@uni-berlin.de:
>
> "John Harrison" <jo*************@hotmail.com> wrote in message
> news:opsas0q1jt212331@andronicus...
>> 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...


Isn't the point is that there isn't a search path? Once you use 'using
namespace xxx;' within the global namespace all the names in xxx *are*
global.


I would have figured that the "search path" would be used for unqualified
names. Much like specifying an executable in Windows or Unix. Just type
the name, and it searches $PATH for the executable. But if you specify a
full pathname to the file (like /program), then you mean just the
executable in the root directory and no other.
Jul 22 '05 #22

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

Similar topics

18
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...
11
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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,...
0
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...

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.