C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Nested Namespaces

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Michael D. Borghardt
Guest





PostPosted: Fri Sep 19, 2003 9:59 am    Post subject: Nested Namespaces Reply with quote



Hi.

I have been looking into nested namespace and from looking at the draft
standard I believe all of this is legal. I have compiled this with VC.NET
and only namespace C creates a problem(see below).

Any thoughts

#include <iostream>

using std::cout;

using std::endl;

// Outer namespace

namespace A

{

// Basic inner namespace

namespace B

{

void foo(int) { cout << "A::B::foo(int)" << endl;}

}

// Make namespace B visible inside namespace C and overload function foo

namespace C

{

using namespace B;

void foo(int, int) { cout << "A::C::foo(int, int)" << endl;};

void foobar()

{

cout << "C::foobar" << endl;

foo(1); // This should call A::B::foo(int) <====== I get compiler error
here

}

}

// Make B::foo visible inside namespace C with using declaration and
overload function foo

namespace D

{

void foo(int, int);

using B::foo;;

void foobar()

{

cout << "D::foobar" << endl;

foo(1); // This should call A::B::foo(int)

}

}

// Using explcit qualification to call B::foo(int)

namespace E

{

void foo(int, int);

void foobar()

{

cout << "E::foobar" << endl;

B::foo(1);

}

}

// Make namespace B visible with using declaration

using namespace B;

// Determine if nested namespace visible when using parent namespace using
declaration

namespace F

{

void foo(int, int);

using A::foo;;

void foobar()

{

cout << "F::foobar" << endl;

foo(1); // This should call A::B::foo(int)

}

}

// Determine if nested namespace visible when using explcit qualification

namespace G

{

void foo(int, int);

void foobar()

{

cout << "G::foobar" << endl;

A::foo(1);

}

}

}

void Test()

{

A::C::foobar();

A:Very Happy::foobar();

A::E::foobar();

A::F::foobar();

A::G::foobar();

}




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Richard Smith
Guest





PostPosted: Fri Sep 19, 2003 11:20 am    Post subject: Re: Nested Namespaces Reply with quote



Michael D. Borghardt wrote:

[Code reformatted]

Quote:
namespace A {
namespace B {
void foo(int) { cout << "A::B::foo(int)" << endl;}
}
namespace C {
using namespace B;

The problem is that you are misunderstanding what a using
directive does. A using directive (as opposed to a using
declaration) makes names visible as if they were declared in
the inner most namespace enclosing both the source namespace
(A::B) and the target namespace (A::C). In this example,
the common enclosing namespace is A, and foo is injected
into namespace A.

Quote:
void foo(int, int) { cout << "A::C::foo(int, int)" << endl;};

This hides (not overloads) the version imported by the using
directive.

Quote:
void foobar() {
cout << "C::foobar" << endl;
foo(1); // This should call A::B::foo(int)
// <====== get compiler error here

The compiler is correct to give you an error. Only one
function foo can be found through ordinary lookup, and that
is the two argument function in namespace A::C. As ADL does
not occur in this case (int has no associated namespaces),
overload resolution fails.

--
Richard Smith

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Fri Sep 19, 2003 5:24 pm    Post subject: Re: Nested Namespaces Reply with quote



In article <4Nlab.174487$la.3607551 (AT) news1 (DOT) calgary.shaw.ca>, Michael D.
Borghardt <michael (AT) borghardtConsulting (DOT) net> writes
Quote:
I have been looking into nested namespace and from looking at the draft
standard I believe all of this is legal. I have compiled this with VC.NET
and only namespace C creates a problem(see below).

Is it just the settings on my newsreader or did you write all your
source code without any indentation but with a blank line between each
line of source code?

I suspect it is the latter. In which case I have no intention of trying
to read it. If it is the former, I apologise and will have to try to
determine what I am doing wrong.

IOWs if you want us to look at your code please present it in a form
that makes the structure clear and keeps as much as possible on the
screen.



--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
White Wolf
Guest





PostPosted: Sat Sep 20, 2003 10:06 am    Post subject: Re: Nested Namespaces Reply with quote

Francis Glassborow wrote:
Quote:
IOWs if you want us to look at your code please present it in a form
that makes the structure clear and keeps as much as possible on the
screen.

I think both Visual Studio and Emacs is able to reformat code. I also see a
suspiciously lot of such unindented two-lines line spacing code with my out
of luck xpress. It might be a copy paste error. I have seen Windows taking
out all indentation and placing those empty lines when copying from HTML.
If this has something to do with copying from a Rich Text control to a Text
Only one it is possible guys just give up on reformatting when they find out
that it is not possible to restore the original structure.

If you use some editor/IDE which supports code reformatting you can still
look at the code. It usually takes about 2-3 seconds time in addition to
the otherwise necessary work (for example if you want to fix/test the code).

Just my 2c (or not to 2c Wink ).

--
WW aka Attila



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Sat Sep 20, 2003 3:18 pm    Post subject: Re: Nested Namespaces Reply with quote

In article <bkfoqg$ss7$1 (AT) phys-news1 (DOT) kolumbus.fi>, White Wolf
<wolof (AT) freemail (DOT) hu> writes
Quote:
Francis Glassborow wrote:
IOWs if you want us to look at your code please present it in a form
that makes the structure clear and keeps as much as possible on the
screen.

I think both Visual Studio and Emacs is able to reformat code. I also see a
suspiciously lot of such unindented two-lines line spacing code with my out
of luck xpress. It might be a copy paste error. I have seen Windows taking
out all indentation and placing those empty lines when copying from HTML.
If this has something to do with copying from a Rich Text control to a Text
Only one it is possible guys just give up on reformatting when they find out
that it is not possible to restore the original structure.

If you use some editor/IDE which supports code reformatting you can still
look at the code. It usually takes about 2-3 seconds time in addition to
the otherwise necessary work (for example if you want to fix/test the code).

It takes considerably longer, I have to load up the relevant
application, cut and paste the code etc. Note that all this is something
that the originally poster found too tedious to do when posting.

If someone wants us to look at their code they should go the extra mile
to make that code readable at sight. Many of us already donate a great
deal of time to helping, the least those wanting help can do is to make
every effort to reduce the time it takes us to understand the problem.

I am tempted to suggest that the moderators should reject posts with
code that needs reformatting before it can be read.


--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Siemel Naran
Guest





PostPosted: Sun Sep 21, 2003 10:13 am    Post subject: Re: Nested Namespaces Reply with quote

"Richard Smith" <richard (AT) ex-parrot (DOT) com> wrote in message
Quote:
Michael D. Borghardt wrote:

namespace A {
namespace B {
void foo(int) { cout << "A::B::foo(int)" << endl;}
}
namespace C {
using namespace B;

Would

using B::foo;

make the program compile? (My compiler is behind the times here, so I can't
test it).

--
+++++++++++
Siemel Naran


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.