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

Changing namespaces

P: n/a
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
Share this Question
Share on Google+
21 Replies


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

P: n/a

"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

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

P: n/a

"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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.