(ref.doc)bs 080294
Next ark 010394
Prev: daniels 040294
Up: Usenet
From: [email protected] (Bjarne Stroustrup)
Newsgroups: comp.std.c++
Subject: Re: Using namespaces (even more)
Date: 8 Feb 94 22:23:20 GMT
> The Resolution-paper r.3.3.1.1 says:
> >...
> >using-declaration:
> > using Namespacename::Identifier;
> >using-directive:
> > using namespace Namespacename;
> >...
> 1) May the Identifier in the using-declaration be a namespace?
Yes.
> 2) Why do I have to write 'using namespace X;' and not just 'using X;'? The
> compiler will always be able to determine if X is a namespace or something
> else. Or is it for some reason important to distinguish lexically between
> using-declaration and using-directive? IMHO writing 'namespace' is just
> superflous. Please correct me.
I tried ``using X;'' It led to confusion among users. ``using namespace X;''
makes it easier for people to grasp the difference between a using-directive
and a using-declaration. Also, implementors found the more explicit form
preferable for parsing, error handling, etc.
> 3) r.3.3.1.3 says:
> >... Unnamed namespaces make global static (r.7.1.1) redundant....
>
> Does this mean that function and variable names that are members of
> an unnamed namespace will not appear in the objectfile and thus
> not be accessable from outside this compilation-unit (like it is
> the case with global static fcts/vars)?
Yes, though there is nothing in C or C++ that prohibits linkers from
making such names accessible to a debugger; they just cannot interfere
with names from other compilation units.
> 4) This is rather an implementation-question than a language-question,
> but it would help understanding how namespaces work:
> a) Will the namespaces of identifiers be encoded into their names in
> the objectifile, so that the linker can ditinginguish between A::var1
> and B::var1?
A::var1 and B::var1 must be distinguished in the object file.
> b) But if so, how can I wrap a namespace around a library/object-file
> (that was compiled without namespaces) afterwads? E.g.:
> namespace XLibrary {
> #include <Xlib.h>
> }
> Or does this result in a linker-error as it wount find
> XLibrary::XOpenDisplay()?
You can't except if the library has C linkage. Systematic use of namespaces
requires cooperation from library suppliers or access to the source code.
> 5) In some articles #include <header> was mentioned. As far as I
> understood it
> #include <header.h> works like it always worked, and
> #include <header> wraps the declarations that are
> in the file 'header.h' into a namespace called 'header'.
> - Please corect me if I'm not right.
You are right.
> My question is:
> How does header.h look like. Do there have to be any namespace or
> using declarations(or -directives or whatever) so that the preprocessor
> (or who does it) is able to do #include <header>?
> Can anyone give me a kind of rewrite-rule like:
> #include <header> => namespace header {
> #include <header.h> // the old textual #include
> }
> (This interferes with question 4)
> Or if there has to be a namespace-wrapper _inside_ header.h:
> namespace header {
> #include <old_header.h>
> }
> Than the rewrite-rule could be:
> #include <header.h> => #include <header.h> // old textual #include
> using header;
The general idea is to have
// header:
namespace header {
#include <header.h> // the old textual #include
}
using namespace header;
but the exact details are not fixed by the standard (except for the
standard headers) and the details may vary from header to header.
> 6) Have there been any considerations to distinguish between public and
> private members of a namespace? This mechanism could be used like private
> class-members to controll access to critical functions (E.g. recursive
> functions that will only terminate with some special parameter-values)
> E.g.:
>
> namespace X {
> private:
> const int MAX = 9999;
> int impl (int a, int b, int c, int d);
> public:
> inline int fct0 (int b) { return impl(0,b,0,MAX); }
> inline int fct1 (int b) { return impl(1,b,1,MAX); }
> inline int rfct0 (int a) { return impl(a,0,MAX,0); }
> inline int rfct1 (int a) { return impl(a,1,MAX,1); }
> }
>
> I would appreciate if this mechanism could be introduced
> analogously(?spelling?) with classes (well, what would 'protected'
> be then?)
Yes. We considered that and many other variants. Most variants were rejected
to avoid adding complications.
I think your example is best handled like this:
namespace X_impl { // implementation details for X
const int MAX = 9999;
int impl(int a, int b, int c, int d);
}
namespace X {
inline int fct0(int b) { return X_impl::impl(0,b,0,X_impl::MAX); }
inline int fct1(int b) { return X_impl::impl(1,b,1,X_impl::MAX); }
inline int rfct0(int a) { return X_impl::impl(a,0,X_impl::MAX,0); }
inline int rfct1(int a) { return X_impl::impl(a,1,X_impl::MAX,1); }
}
automatically generated by info2www version 1.2.2.8