This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: namespace std::
On Wed, Aug 04, 2004 at 09:24:41PM +0530, Dhruv Matani wrote:
> On Wed, 2004-08-04 at 20:09, Jonathan Wakely wrote:
> > On Wed, Aug 04, 2004 at 08:06:39PM +0530, Dhruv Matani wrote:
> >
> > > Hello,
> > > I wanted to know whether:
> > > 1. By including cstdlib I should get access to malloc() and free().
> > > 2. Should malloc() and free() be in namespace std::?
> >
> > Yes and yes.
> >
> > i.e. this should compile fine (and does with GCC)
> >
> > #include <cstdlib>
> > int main()
> > {
> > std::free(std::malloc(1));
> > }
>
> Unfortunately, even this does, which shouldn't:
>
> #include <cstdlib>
> int main()
> {
> free(malloc(1));
> }
No, that shouldn't, and yes, it does :-(
I misunderstood your origianl post. If you meant should malloc() and
free() _only_ be in std, not in the global namespace, you're right.
And GCC gets that wrong on GNU/Linux and FreeBSD, among others.
The reason for this is that libstdc++ uses the system's C headers rather
than reimplementing them. libstdc++ adds a <cxxx> header that includes
the corresponding <xxx.h> header and pulls the names from the global
namespace into the std namespace. This is backwards as far as the
standard is concerned: the names should be declared in std and pulled
into the global namespace, not the other way around. This means that
including a <cxxx> header declares the names in std _and_ in the global
namespace (should only be in std), and including the <xxx.h> header
only declares the names in the global namespace (should be in std too).
For background on the problem see Nathan's
http://www.cantrip.org/cheaders.html
There are worse consequences than the one you note above, such as
<math.h> not declaring the abs() overloads that are required by the
standard, e.g.
#include <math.h>
int main()
{
return ::abs(0.0); // should be OK, but errors
}
This is because the system's math.h does not support C++ and does not
declare the abs() overloads added by ISO 14882. Since the user has
included <math.h> not <cmath> no libstdc++ header is included so there
is no chance to add the overloads specific to C++. This sucks.
jon
--
"Never ascribe to malice what can be explained by incompetence."
- Napoleon (?)