Topic: namespace, question repost
Author: hyuan@runner.ucdavis.edu (Heng Yuan)
Date: Fri, 8 Sep 2000 18:23:00 GMT Raw View
Assume that I have
using namespace abc;
using namespace def;
somewhere in the beginning. A few functions in abc
and def have the same function prototype. It would
be fine if I don't use these functions, or in the
specific places, I have
using abc::operator<<;
using abc::operator>>;
using abc::getline
the above 3 statements would solve the name clashing
problem for the above 3 functions. However, what if
there are more clashes, won't it be nice to have
something like:
using namespace abc
{
cin >> a;
cout << a;
getline (cin, linebuf);
};
? Possible?
Please don't tell me to use
{
using namespace abc;
}
It's not going to work. There are significant
differences between these two.
Heng Yuan
heng@ag.arizona.edu
hyuan@ucdavis.edu
---
[ 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: Jim Hyslop <jim.hyslop@leitch.com>
Date: Sat, 9 Sep 2000 13:42:11 GMT Raw View
In article <8patll$nsh$1@mark.ucdavis.edu>,
hyuan@runner.ucdavis.edu (Heng Yuan) wrote:
> Assume that I have
> using namespace abc;
> using namespace def;
> somewhere in the beginning. A few functions in abc
> and def have the same function prototype. It would
> be fine if I don't use these functions, or in the
> specific places, I have
> using abc::operator<<;
> using abc::operator>>;
> using abc::getline
> the above 3 statements would solve the name clashing
> problem for the above 3 functions. However, what if
> there are more clashes, won't it be nice to have
> something like:
> using namespace abc
> {
> cin >> a;
> cout << a;
> getline (cin, linebuf);
> };
> ? Possible?
> Please don't tell me to use
> {
> using namespace abc;
> }
> It's not going to work. There are significant
> differences between these two.
I don't understand you - the second form you have shown will do
*exactly* what you have just asked. For example:
#include <iostream>
void f()
{
using namespace std;
cout << "Hello, C++ world!\n" << endl;
}
int main()
{
cout << "Hello, C++ world!\n" << endl;
f();
}
f() should compile without error, but main() should give you errors that
'cout' and 'endl' are undeclared identifiers. Isn't that what you want?
--
Jim
This message was posted using plain text only. Any hyperlinks you may
see were added other parties without my permission.
I do not endorse any products or services that may be hyperlinked to
this message.
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ 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: comeau@panix.com (Greg Comeau)
Date: Mon, 11 Sep 2000 23:08:01 GMT Raw View
In article <8phgu4$cu6$1@mark.ucdavis.edu>,
Heng Yuan <hyuan@runner.ucdavis.edu> wrote:
>.... Imagine that there are a number of
>namespaces which are enabled ... there are a few functions
>in these namespaces share the same prototype.....
>So I propose something like:
> using namespace abc
> {
> cout << obj;
> }
>This form forces functions insides the brackets in abc take
>precedence over global and other namespace functions. It is
>very different from
> using namespace abc;
>which just tells the compiler to load the function prototypes
>of abc as part of default function lookup table.
I know where you are coming from, and other languages have
something similar in some cases, but assuming that I'm
understanding what it is that you want to do I just don't really
see the magic bullet given the constraints you've laid out here,
that is, not over say just writing a function to do the same thing.
- Greg
--
Comeau Computing / Comeau C/C++ ("so close" 4.2.44 betas starting)
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.com
---
[ 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: hyuan@runner.ucdavis.edu (Heng Yuan)
Date: Mon, 11 Sep 2000 02:59:40 GMT Raw View
You got me wrong totally. Imagine that there are a number of
namespaces which are enabled by
using namespace abc;
using namespace def;
using namespace ghi;
However, there are a few functions in these namespaces share
the same prototype. Thus, in order to use the specific functions,
I would have to do either:
using abc::operator<<;
cout << obj;
or
abc::operator<< (cout, obj);
. However, if I have a block of codes which involves more than
one functions, it would be extremely tedious to write them these
ways. Also, if I want to switch the namespace functions, it
would be a pain to change all of them. So I propose something
like:
using namespace abc
{
cout << obj;
}
This form forces functions insides the brackets in abc take
precedence over global and other namespace functions. It is
very different from
using namespace abc;
which just tells the compiler to load the function prototypes
of abc as part of default function lookup table.
In article <8pbmg6$obk$1@nnrp1.deja.com>,
Jim Hyslop <jim.hyslop@leitch.com> wrote:
>In article <8patll$nsh$1@mark.ucdavis.edu>,
> hyuan@runner.ucdavis.edu (Heng Yuan) wrote:
>> Assume that I have
>> using namespace abc;
>> using namespace def;
>> somewhere in the beginning. A few functions in abc
>> and def have the same function prototype. It would
>> be fine if I don't use these functions, or in the
>> specific places, I have
>> using abc::operator<<;
>> using abc::operator>>;
>> using abc::getline
>> the above 3 statements would solve the name clashing
>> problem for the above 3 functions. However, what if
>> there are more clashes, won't it be nice to have
>> something like:
>> using namespace abc
>> {
>> cin >> a;
>> cout << a;
>> getline (cin, linebuf);
>> };
>> ? Possible?
>> Please don't tell me to use
>> {
>> using namespace abc;
>> }
>> It's not going to work. There are significant
>> differences between these two.
>I don't understand you - the second form you have shown will do
>*exactly* what you have just asked. For example:
>
>#include <iostream>
>
>void f()
>{
> using namespace std;
> cout << "Hello, C++ world!\n" << endl;
>}
>
>int main()
>{
> cout << "Hello, C++ world!\n" << endl;
> f();
>}
>
>f() should compile without error, but main() should give you errors that
>'cout' and 'endl' are undeclared identifiers. Isn't that what you want?
>
>--
>Jim
>This message was posted using plain text only. Any hyperlinks you may
>see were added other parties without my permission.
>I do not endorse any products or services that may be hyperlinked to
>this message.
>
>
>Sent via Deja.com http://www.deja.com/
>Before you buy.
>
>---
>[ 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 ]
>
---
[ 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 ]