Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!decwrl!world!wmm From: wmm@world.std.com (William M Miller) Newsgroups: comp.lang.c++ Subject: Re: namespace (rethought & reiterated) Message-ID: <1991Mar3.193216.7474@world.std.com> Date: 3 Mar 91 19:32:16 GMT References: <2620020@otter.hpl.hp.com> <4196@lupine.NCD.COM> Organization: The World Public Access UNIX, Brookline, MA Lines: 91 rfg@NCD.COM (Ron Guilmette) writes: > Consider a header file like: > > incl.h: > -------------------------------------------------------------------- > extern int somedata; > > typedef double* double_p_type; > > extern void myfunc (double, char); > -------------------------------------------------------------------- > > Ask yourself if there are any reasons why this could not be rephrased as: > > incl.h: > -------------------------------------------------------------------- > class incl { > public: > static int somedata; > > typedef double* double_p_type; > > static void myfunc (double, char); > }; > -------------------------------------------------------------------- > > Now if we include this into some other file (or files) we should be able > to refer to `incl::somedata', `incl::double_p_type', and `incl::myfunc'. > The problem with this scheme is that we are now *forced* to *always* > provide explicit qualification for anything we get from `incl.h'. > Is there a solution? Sure. We could introduce a `with' statement (as in > Pascal) which could specify that within a given region, a particular class > type should be used for implicit qualification (when necessary) I don't think a "with" statement is really necessary. Consider the following version of incl.h: incl.h ----------------------------------------------------------------- class incl { public: static int somedata; typedef double* double_p_type; static void myfunc(double, char); }; #ifndef QUALIFIED int& somedata = incl::somedata; typedef incl::double_p_type double_p_type; void (*myfunc)(double, char) = incl::myfunc; #endif ----------------------------------------------------------------- With this kind of header, if you don't have name conflicts, you simply include the header and use the unqualified names. If you do have conflicts, simply #define QUALIFIED before including the header and only the "incl::" qualified names will be seen (of course, there's nothing to stop a compilation unit from using the QUALIFIED version and extracting via typedef, reference, or function pointer any frequently-used non-conflicting individual names). This corresponds roughly to qualified and unqualified export in Modula-2. Another thought that corresponds more closely to "with" would be to put the stuff in the "#ifdef QUALIFIED" section into another include file, say, withincl.h; incl.h would have only the "incl::" qualified names. Ron's "with" example becomes something like: -------------------------------------------------------------------- #include double d; void foobar () { { #include double_p_type dp = &d; myfunc (dp, (char) somedata); } } -------------------------------------------------------------------- -- William M. Miller, Glockenspiel, Ltd. wmm@world.std.com