 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
nick@byu.edu Guest
|
Posted: Sun Jun 29, 2003 11:29 am Post subject: can't get div(int, int) out of global namespace |
|
|
I'm writing my own function int div(int, int).
Including any of:
<algorithm>
<cstdlib>
<stdlib>
all cause struct div_t div(int, int) to be put into the _global_
namespace. (My understanding is that those should be pulled in by
<math.h> or <cmath>, and if by <cmath> then into std::.) Even
attempting to #include any of the above inside a namespace std { }
block doesn't solve it.
I don't care if struct div_t div(int, int) is in std:: --that would be
great. I just want to get it out of the global namespace so I can
use my own div() w/o having to put it in it's own namespace, and then
consequently have to disambiguate it everywhere.
Is there a way to do this?
(gnu gcc 2.95.3, glibc 2.2.4 if it matters)
Nick
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sebastian Kapfer Guest
|
Posted: Sun Jun 29, 2003 9:15 pm Post subject: Re: can't get div(int, int) out of global namespace |
|
|
On Sun, 29 Jun 2003 07:29:58 -0400, nic wrote:
| Quote: | Is there a way to do this?
(gnu gcc 2.95.3, glibc 2.2.4 if it matters)
|
gcc 2.9x has broken namespaces. std::div will always be ::div using that
compiler. Updating to 3.x helps (3.2+ is to be preferred, less bugs).
You
can still overload the function though, if it takes arguments different
from std::div.
And even if it takes the same arguments, it *might* work. The linker
should prefer functions from .o files to those in .a files. You'll have
to
declare your function extern "C" possibly. This is pretty shaky
terrain...
do you really have to call the function "div"?
--
Best Regards, | Hi! I'm a .signature virus. Copy me into
Sebastian | your ~/.signature to help me spread!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Early Ehlinger Guest
|
Posted: Sun Jun 29, 2003 10:16 pm Post subject: Re: can't get div(int, int) out of global namespace |
|
|
<nick (AT) byu (DOT) edu> wrote:
| Quote: | Is there a way to do this?
(gnu gcc 2.95.3, glibc 2.2.4 if it matters)
|
It does :)
gcc 2.95.x didn't really have its library in std::, IIRC. Instead,
everything was in namespace "::" and if you referenced std:: you would
get
::.
Best course of action? Upgrade to 3.x.
--
-- Early Ehlinger CEO, ResPower Inc - Toll-Free : 866-737-7697
-- www.respower.com -- 500+ GHz Supercomputer Starting At
USD$0.50/GHz*Hour
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Sun Jun 29, 2003 10:19 pm Post subject: Re: can't get div(int, int) out of global namespace |
|
|
[email]nick (AT) byu (DOT) edu[/email] wrote:
| Quote: | I don't care if struct div_t div(int, int) is in std:: --that would be
great. I just want to get it out of the global namespace so I can
use my own div() w/o having to put it in it's own namespace, and then
consequently have to disambiguate it everywhere.
Is there a way to do this?
(gnu gcc 2.95.3, glibc 2.2.4 if it matters)
|
First thing with such problems is to check the bug-database of the
compiler. There you will quickly find that the version is ancient and
that
corrections to the (non-standard) stdlib are not done in order not to
break any old software. IOW: upgrade your compiler.
Second thing is that you hopefully don't want to pollute the global
namespace yourself with such a universal name, no ?
hth
Ulrich Eckhardt
--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Sun Jun 29, 2003 10:21 pm Post subject: Re: can't get div(int, int) out of global namespace |
|
|
<nick (AT) byu (DOT) edu> wrote
| Quote: | I'm writing my own function int div(int, int).
Including any of:
algorithm
cstdlib
stdlib
all cause struct div_t div(int, int) to be put into the _global_
namespace. (My understanding is that those should be pulled in by
math.h> or <cmath>, and if by <cmath> then into std::.) Even
attempting to #include any of the above inside a namespace std { }
block doesn't solve it.
I don't care if struct div_t div(int, int) is in std:: --that would be
great. I just want to get it out of the global namespace so I can
use my own div() w/o having to put it in it's own namespace, and then
consequently have to disambiguate it everywhere.
|
Can you put your div(int,int) into its own namespace?
Though it seems your system headers are faulty. In stdlib.h there is a
function div(int, int) in the global namespace. But in cstdlib this
function is in the standard namespace. If you include cstdlib, and
header
files like algorithm include only cstdlib, then it should work. My
guess is
that your system headers are including stdlib.h somewhere. I prove this
by
writing this program
int div(int, int)
{ // line 2
return 0;
}
void action()
{
div(3,2);
}
#include <algorithm> // line 11
#include <cstdlib>
int main()
{
action();
}
In file included from
/usr/lib/gcc-lib/i686-pc-cygwin/2.95.2-6/../../../../inclu
de/g++-3/stl_algobase.h:50,
from
/usr/lib/gcc-lib/i686-pc-cygwin/2.95.2-6/../../../../inclu
de/g++-3/algorithm:30,
from h.cc:11:
/usr/include/stdlib.h:61: new declaration `struct div_t div(int, int)'
h.cc:2: ambiguates old declaration `int div(int, int)'
Line 50 of stl_algobase.h is
#include <stdlib.h>
On Borland VC6 I find the same thing, but only if I include <vcl.h>.
This
is the file that defines __InitVCL and __ExitVCL, which you need in
order to
create the executable, and are specific to Borland. Doubtless, the
header
<vcl.h> is faulty -- they must be including stdlib.h rather than
cstdlib.
Solution here is to move the #include <vcl.h> till just before int
main().
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Sun Jun 29, 2003 10:22 pm Post subject: Re: can't get div(int, int) out of global namespace |
|
|
Francis Glassborow wrote:
| Quote: | But exactly why are you so determined to use the name 'div' at global
level? What is wrong with 'mydiv' or 'Div' or 'divx' etc.
|
I'll bet he's working on the 2003 ICFP contest :-)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Sun Jun 29, 2003 10:22 pm Post subject: Re: can't get div(int, int) out of global namespace |
|
|
On 29 Jun 2003 07:29:58 -0400, [email]nick (AT) byu (DOT) edu[/email] wrote in
comp.lang.c++.moderated:
| Quote: | I'm writing my own function int div(int, int).
Including any of:
algorithm
cstdlib
stdlib
all cause struct div_t div(int, int) to be put into the _global_
namespace. (My understanding is that those should be pulled in by
math.h> or <cmath>, and if by <cmath> then into std::.) Even
attempting to #include any of the above inside a namespace std { }
block doesn't solve it.
I don't care if struct div_t div(int, int) is in std:: --that would be
great. I just want to get it out of the global namespace so I can
use my own div() w/o having to put it in it's own namespace, and then
consequently have to disambiguate it everywhere.
Is there a way to do this?
(gnu gcc 2.95.3, glibc 2.2.4 if it matters)
Nick
|
Gcc 2.95 is quite old, and does not conform to standard C++ namespace
usage properly. You might have better luck with a current version
(3.3.something, I think).
But, as Francis pointed out, why can't you just name your function
something else?
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Mon Jun 30, 2003 4:47 am Post subject: Re: can't get div(int, int) out of global namespace |
|
|
Sebastian Kapfer <sebastian_kapfer (AT) web (DOT) de> writes:
| Quote: | On Sun, 29 Jun 2003 07:29:58 -0400, nic wrote:
Is there a way to do this?
(gnu gcc 2.95.3, glibc 2.2.4 if it matters)
gcc 2.9x has broken namespaces. std::div will always be ::div using
that
compiler. Updating to 3.x helps (3.2+ is to be preferred, less bugs).
You
can still overload the function though, if it takes arguments
different
from std::div.
And even if it takes the same arguments, it *might* work.
|
Please notice that it is unspecified whether funtions from the "C"
library have C or C++ linkage. Therefore, insisting on defining ones
own function named div with the same signature as the standard one is
highly unportable and asking for trouble.
--
Gabriel Dos Reis, [email]gdr (AT) integrable-solutions (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Mon Jun 30, 2003 4:47 am Post subject: Re: can't get div(int, int) out of global namespace |
|
|
"Early Ehlinger" <early (AT) respower (DOT) com> writes:
| Quote: | Best course of action? Upgrade to 3.x.
|
Does that really makes the core issue go away?
--
Gabriel Dos Reis, [email]gdr (AT) integrable-solutions (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Mon Jun 30, 2003 4:48 am Post subject: Re: can't get div(int, int) out of global namespace |
|
|
"Siemel Naran" <SiemelNaran (AT) KILL (DOT) att.net> writes:
| Quote: | nick (AT) byu (DOT) edu> wrote
I'm writing my own function int div(int, int).
Including any of:
algorithm
cstdlib
stdlib
all cause struct div_t div(int, int) to be put into the _global_
namespace. (My understanding is that those should be pulled in by
math.h> or <cmath>, and if by <cmath> then into std::.) Even
attempting to #include any of the above inside a namespace std { }
block doesn't solve it.
I don't care if struct div_t div(int, int) is in std:: --that would
be
great. I just want to get it out of the global namespace so I can
use my own div() w/o having to put it in it's own namespace, and
then
consequently have to disambiguate it everywhere.
Can you put your div(int,int) into its own namespace?
|
That is no better. In case the standard div has a C linkage.
--
Gabriel Dos Reis, [email]gdr (AT) integrable-solutions (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
llewelly Guest
|
Posted: Mon Jun 30, 2003 4:48 am Post subject: Re: can't get div(int, int) out of global namespace |
|
|
[email]nick (AT) byu (DOT) edu[/email] writes:
| Quote: | I'm writing my own function int div(int, int).
Including any of:
algorithm
cstdlib
stdlib
all cause struct div_t div(int, int) to be put into the _global_
namespace. (My understanding is that those should be pulled in by
math.h> or <cmath>, and if by <cmath> then into std::.) Even
attempting to #include any of the above inside a namespace std { }
block doesn't solve it.
I don't care if struct div_t div(int, int) is in std:: --that would be
great. I just want to get it out of the global namespace so I can
use my own div() w/o having to put it in it's own namespace, and then
consequently have to disambiguate it everywhere.
Is there a way to do this?
(gnu gcc 2.95.3, glibc 2.2.4 if it matters)
[snip] |
Upgrade to gcc 3.x
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Mon Jun 30, 2003 9:49 am Post subject: Re: can't get div(int, int) out of global namespace |
|
|
Gabriel Dos Reis wrote:
| Quote: | "Siemel Naran" <SiemelNaran (AT) KILL (DOT) att.net> writes:
| <nick (AT) byu (DOT) edu> wrote
| Can you put your div(int,int) into its own namespace?
That is no better. In case the standard div has a C linkage.
|
Did you really mean what it seems you meant ? Is this bad in any way ?
<file cstdlib>
extern "C" int(div(int,int);
<file foo.cpp>
namespace phoob4r
{
int div(int,int);
}
Ulrich Eckhardt
--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Mon Jun 30, 2003 4:05 pm Post subject: Re: can't get div(int, int) out of global namespace |
|
|
Ulrich Eckhardt <doomster (AT) knuut (DOT) de> writes:
| Quote: | Gabriel Dos Reis wrote:
"Siemel Naran" <SiemelNaran (AT) KILL (DOT) att.net> writes:
| <nick (AT) byu (DOT) edu> wrote
| Can you put your div(int,int) into its own namespace?
That is no better. In case the standard div has a C linkage.
Did you really mean what it seems you meant ? Is this bad in any way ?
file cstdlib
extern "C" int(div(int,int);
file foo.cpp
namespace phoob4r
{
int div(int,int);
}
|
::div and phoob4r::div are the same. See 7.5/6 and 7.5/5.
--
Gabriel Dos Reis, [email]gdr (AT) integrable-solutions (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Mon Jun 30, 2003 4:45 pm Post subject: Re: can't get div(int, int) out of global namespace |
|
|
Daniel Spangenberg <dsp (AT) bdal (DOT) de> writes:
| Quote: | Hello, Gabriel!
Gabriel Dos Reis schrieb:
[snip9
Please notice that it is unspecified whether funtions from the "C"
library have C or C++ linkage. Therefore, insisting on defining
ones
own function named div with the same signature as the standard one
is
highly unportable and asking for trouble.
You are abolutely right. But I think, he could use a SFINAE variant,
which I proposed for compile time tests of the available ::std::abs
and
::abs signatures as well as linkage types. The implementation is
mainly
based on ideas of Rani Sharoni.
|
How can SFINAE solve that problem in a reliable way?
Suppose the program P is composed of two translation units TU1 and TU2.
Further assume that TU1 #includes a standard header that brings a
declaration of ::div with a C linkage. Also assume that in TU2 he
defines "successfully" his own ::div with the same signature (but now
with a C++ linkage), then 7.5/5 applies and that is an ODR violation.
| Quote: | If you are interested, have a Google for
"C++98 compliant abs/fabs signature tester? (LONG) "
or combine the Words "Spangenberg Unique abs".
|
Thanks, I'll check.
--
Gabriel Dos Reis, [email]gdr (AT) integrable-solutions (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sebastian Kapfer Guest
|
Posted: Mon Jun 30, 2003 6:36 pm Post subject: Re: can't get div(int, int) out of global namespace |
|
|
On Mon, 30 Jun 2003 00:48:09 -0400, Gabriel Dos Reis wrote:
| Quote: | | > great. I just want to get it out of the global namespace so I can
| > use my own div() w/o having to put it in it's own namespace, and then
| > consequently have to disambiguate it everywhere.
|
| Can you put your div(int,int) into its own namespace?
That is no better. In case the standard div has a C linkage.
|
If user::div has C++ linkage (or no linkage at all, e.g. inline/static), I
can see no problem.
--
Best Regards, | Hi! I'm a .signature virus. Copy me into
Sebastian | your ~/.signature to help me spread!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|