help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Does cstdlib pollute the global namespace?


From: gpderetta
Subject: Re: Does cstdlib pollute the global namespace?
Date: Tue, 30 Oct 2007 17:41:43 -0000
User-agent: G2/1.0

On Oct 18, 8:07 pm, Pedro LamarĂ£o <pedro.lama...@gmail.com> wrote:
> On Oct 18, 2:58 pm, Jeff Schwab <j...@schwabcenter.com> wrote:
>
>
>
> > Is the following error message correct?  It seems like system() ought
> > to be in the ::std namespace.  What am I missing?
>
> >     // main.cc
> >     #include <cstdlib>
> >     namespace system { }
>
> >     $ g++ -ansi -pedantic -Wall -c main.cc
> >     main.cc:3: error: 'namespace system { }' redeclared as different
> > kind of symbol
> >     /usr/include/stdlib.h:738: error: previous declaration of 'int
> > system(const char*)'
> >     make: *** [main.o] Error 1
>
> > My current work-around is an unnamed "wrapper" namespace:
>
> >     namespace { namespace system { } }
>
> This behaviour is alloweb by the Standard.
>
> In the section [headers] paragraph 4 of the current draft we read:
>
> "Except as noted in clauses 18 through 27 and Annex D the contents of
> each header cname shall be the same as that of
> the corresponding header name.h, as specified in the Standard C99
> Library (1.2) or the C Unicode TR, as appropriate, as
> if by inclusion. In the C++ Standard Library, however, the
> declarations (except for names which are defined as macros
> in C) are within namespace scope (3.3.5) of the namespace std. It is
> unspecified whether these names are first declared
> within the global namespace scope and are then injected into namespace
> std by explicit using-declaration s (7.3.3)."
>
> That last sentence means the compiler is allowed to declare these
> names in the global namespace.
>
> I don't have the current Standard around to check if the same language
> is in place, but I believe it to be so.
>

IIRC, the permission to inject names into the global namespace has
been
added to the draft standard only recently and was not in the C++03
standard,
to codify existing practice [1]. In fact very few implementations
managed to
put all C declarations in the std namespace, because most had to deal
with
an externally provided libc.

HTH,

[1] I do not own the C++ standard, so I might quite likely be wrong.

--
Giovanni P. Deretta



reply via email to

[Prev in Thread] Current Thread [Next in Thread]