Topic: Namespace question


Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/04/30
Raw View
In article <7g6nr4$2h7$1@pollux.ip-plus.net>, Thomas Bednarz
<tkmb@bluewin.ch> writes
>
>What is the name of the global namespace? If I write the following code:
>
>using namespace stl
>......
>......
>
>using namespace CORBA
>.......
>
>
>
>how do I get back to the global namespace? I need to use 'using' because
>fully qualified names do not work because of some macros which are part of
>external headers I have to include.  They do not expand correctly when using
>things like CORBA::string etc.

I think you are probably misunderstanding how namespaces work.  You have
used two using directives (presumably at global scope) that has made all
the names in those two spaces available at global scope (but not
declared at that level.

If you have macros screwing up your fully qualified names you have, IMO,
an unusable set of header files. Have you considered using undef to hide
the destructive macros.

[ Moderator note: quotation of the signature removed
  please don't overquote --vb ]

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1999/04/30
Raw View
Thomas Bednarz<tkmb@bluewin.ch> wrote:
>What is the name of the global namespace? If I write the following code:
>
>using namespace stl
>using namespace CORBA
>
>how do I get back to the global namespace? I need to use 'using' because
>fully qualified names do not work because of some macros which are part of
>external headers I have to include.  They do not expand correctly when using
>things like CORBA::string etc.

While Thomas might misunderstand namespaces a bit,
a small variation on his question is interesting.

First, namespaces are not just for libraries.  The easiest way to
get around problems with scoping is to create a scope:

  namespace dummy {
    using namespace stl;
    using namespace Corba;

    // your code
  }

That is, put *all* new C++ code in some namespace or other.
Then, you can use sub-namespaces to confine visibility of names.
(A coding standard outlawing C++ code that is not in a namespace
would be not unreasonable.)  Putting your code in a namespace
helps to protect it against collisions with (nonstandard) C
library globals, improving portability.

Second: does this namespace "Corba" really have so many names?
Maybe you would be better off using namepace declarations instead:
"using Corba::open;" etc. rather than "using namespace Corba;".

--
Nathan Myers
ncm@nospam.cantrip.org  http://www.cantrip.org/
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 1999/05/01
Raw View
ncm@nospam.cantrip.org (Nathan Myers) writes:

> Second: does this namespace "Corba" really have so many names?
> Maybe you would be better off using namepace declarations instead:
> "using Corba::open;" etc. rather than "using namespace Corba;".

I'd say it does (assuming we speak about namespace CORBA, as defined
by the OMG C++ language mapping). A rough count in some implementation
gives me about
- 50 classes (e.g. Object, TypeCode, Principal, Request, InterfaceDef,
  ...)
- 1500 typedefs (Object_var, Object_ptr, ..., Long, Short, ...,
  AttributeDescription_out)
- some functions and operators, although most of those would be found
  through Koenig lookup

As for the typedefs, I haven't really checked whether all are mandated
by CORBA, whether there were duplicates, and whether all of them are
in namespace CORBA (the grep showed 2026 typedefs, but some where
clearly local to classes, and some are in the namespace
PortableServer).

So yes, I'd say namespace CORBA does have many names.

Regards,
Martin
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/05/04
Raw View
In article <p6qlnf8zn32.fsf@pandora.inst-inf-1.hu-berlin.de>, Martin von
Loewis <loewis@informatik.hu-berlin.de> writes
>So yes, I'd say namespace CORBA does have many names.

Which, to my mind, is a potent argument AGAINST a using directive.

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Thomas Bednarz" <tkmb@bluewin.ch>
Date: 1999/04/28
Raw View
What is the name of the global namespace? If I write the following code:

using namespace stl
......
......

using namespace CORBA
.......



how do I get back to the global namespace? I need to use 'using' because
fully qualified names do not work because of some macros which are part of
external headers I have to include.  They do not expand correctly when using
things like CORBA::string etc.

Many Thanks.


Thomas




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron.natalie@sensor.com>
Date: 1999/04/28
Raw View
Thomas Bednarz wrote:
>
> What is the name of the global namespace? If I write the following code:
>
> using namespace stl
> ......
> ......
>
> using namespace CORBA
> .......
>
> how do I get back to the global namespace? I need to use 'using' because
> fully qualified names do not work because of some macros which are part of
> external headers I have to include.

There is no name for the global namespace (which is a different
concept from the unnamed namespace).  You seem to be under
the impression that a "using namespace" directive cancels
the previous one.  It does not.  There is no way to undo
a using until you hit the end of the scope it is used in.

Your program adds everything from the "stl" namespace
(whatever that is, perhaps you want std) and the CORBA
namespace as if they were globally defined.  You may wish
to read chapter 8 of Stroustrup.



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Abhijit Gore" <abhijitgore_at_earthlink_dot_net@news.or.intel.com>
Date: 1999/04/28
Raw View

Thomas Bednarz wrote in message <7g6nr4$2h7$1@pollux.ip-plus.net>...
>
>What is the name of the global namespace? If I write the following code:
>
>using namespace stl
>......
>......
>
>using namespace CORBA
>.......
>
>
>
>how do I get back to the global namespace?

you can use scope qualifier "::" to get back to the global namespace.

abhijit gore
abhijitgore_at_earthlink_dot_net




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "John Lund" <jlund@allaire.com>
Date: 1999/04/28
Raw View
Thomas Bednarz wrote in message <7g6nr4$2h7$1@pollux.ip-plus.net>...
>What is the name of the global namespace? If I write the following code:
>using namespace stl

>how do I get back to the global namespace?

I'd suggest trying to localize the namespace qualification as much as you
can. Since the "std::" qualifier doesn't work in your situation you might
try something like the following:


class string{}; // Your nonstandard string.
#include <string>

void f()
{
   using std::string;
   string std_string;

   typedef ::string TMyString; // explicit reference to the global namespace
   TMyString my_string;
}
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]