| View previous topic :: View next topic |
| Author |
Message |
Michael D. Borghardt Guest
|
Posted: Fri Sep 19, 2003 9:59 am Post subject: Nested Namespaces |
|
|
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: ::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
|
Posted: Fri Sep 19, 2003 11:20 am Post subject: Re: Nested Namespaces |
|
|
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
|
Posted: Fri Sep 19, 2003 5:24 pm Post subject: Re: Nested Namespaces |
|
|
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
|
Posted: Sat Sep 20, 2003 10:06 am Post subject: Re: Nested Namespaces |
|
|
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 ).
--
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
|
Posted: Sat Sep 20, 2003 3:18 pm Post subject: Re: Nested Namespaces |
|
|
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
|
Posted: Sun Sep 21, 2003 10:13 am Post subject: Re: Nested Namespaces |
|
|
"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 |
|
 |
|