Topic: namespaces (was a library construction issue - forward references to nested classes)
Author: erc@netcom.com (Eric Smith)
Date: Sun, 11 Apr 1993 18:52:33 GMT Raw View
In article <1993Apr11.140408.25891@ucc.su.OZ.AU> maxtal@physics.su.OZ.AU (John Max Skaller) writes:
> There is a separate proposal for namespace management.
>
> namespace X { f(); }
> using namespace X;
> int i=f();
>
> namespace X { g(); }
> int j=g(); // OK!
>
> namespace Y { k(); }
> int l=Y::k();
>
> namespace {
> int w; // 'static' not needed!
> };
Can we use multiple namespaces simultaneously? And is there another
command after "using" to indicate "no longer using" that namespace?
Can we nest the "using"s? For example,
using namespace X;
int i=f(); // Searches X for f;
using namespace G;
int j=f(); // Searches G, then X, for f.
no longer using namespace G;
no longer using namespace X;
Or maybe the "using"s are nested by the blocks containing them?
{using namespace X;
int i=f(); // Searches X for f;
{using namespace G;
int j=f(); // Searches G, then X, for f.
} // no longer using namespace G
} // no longer using namespace X
And which definition takes precedence when the name is found in both
a namespace and an enclosing block?
Author: lih@cs.columbia.edu (Andrew "Fuz" Lih)
Date: Mon, 12 Apr 1993 00:29:05 GMT Raw View
In article <ercC5C1rL.5sr@netcom.com> erc@netcom.com (Eric Smith) writes:
>
>Can we use multiple namespaces simultaneously? And is there another
>command after "using" to indicate "no longer using" that namespace?
>
>Or maybe the "using"s are nested by the blocks containing them?
> {using namespace X;
> int i=f(); // Searches X for f;
> {using namespace G;
> int j=f(); // Searches G, then X, for f.
> } // no longer using namespace G
> } // no longer using namespace X
>
>And which definition takes precedence when the name is found in both
>a namespace and an enclosing block?
The second is the right usage, the namespace takes effect from the
point of the "using" declaration until it goes out of scope. If a
locally declared name collides with a name in namespace being "used"
in that scope, I believe the local one will always win.
For example:
namespace X {
int x;
};
main()
{
{
using namespace X;
int x=42;
cout << x << endl; // prints local x
}
{
int x=42;
using namespace X;
cout << x << endl; // still prints local x
}
}
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Andrew "Fuz" Lih Columbia University
lih@cs.columbia.edu CRF Tech Staff
"Never let schooling interfere with your education"