[Top][All Lists]

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

Re: GMP C/C++ namespace conflict

From: Hans Aberg
Subject: Re: GMP C/C++ namespace conflict
Date: Mon, 30 Apr 2001 12:24:58 +0200

At 09:46 +1000 2001/04/30, Kevin Ryde wrote:
>> Now, if one is using dedicated namespace programming, one does not want to
>> have this "using namespace std", and once it has appeared in the input,
>> there is no way to nullify its effect on names.
>You mean all the gmp functions end up in "std"?

Strictly speaking I think that C++ uses a namespace path that is set by
"using namespace std". One can still use the full "std::<name>" names.

Here is an example: Say that you inside "gmp" develops a class "list",
which uses the class std::list. Then one can write
  #include <list> // Defines std::list
  namespace gmp {
    class list : public virtual std::list { ... };
However, if you somewhere before that throw in a "using namespace std" you
get a compiler error if you inside namespace "gmp" tries to use the name
"list", because the compiler can interpret it both as gmp::list and
std::list. Then you must write "gmp::list" everywhere.

>> If one should follow this C++ standard library, then gmp.h could add "using
>> namespace std" (which happens because of the inclusion of the <stddef.h>
>> instead of the <cstdlib> header), but the header <gmp> that might be added
>> for use with C++ only should not add "using namespace std".
>I guess that would work.  Is that the usual approach?  I might have
>thought there'd be a lot of old headers supporting c++ only with
>extern "C", so perhaps there's a standard approach to the problem.

On the compiler I have, for say the two headers <cstdlib> and <stdlib.h>,
then <cstdlib> contains all C declarations inside namespace "std" and
extern "C" as follows:
#ifdef __cplusplus
namespace std {
  extern "C" {

  /* C declarations. */

#endif /* __cplusplus */
  } // extern "C"
} //namespace std

The <stdlib.h> then merely contains:
#ifndef _STDLIB_H
#define _STDLIB_H

#include <cstdlib>

#ifdef __cplusplus
  using namespace std;

#endif /* _STDLIB_H */

The effect is that both <cstdlib> and <stdlib.h> reads the same
declarations, but with the latter appending a "using namespace std".

There might be other variations, but this ensures that from the point of
view of the C++ compiler, the standard library names always end up with
namespace std, and not in the global namespace.

Now, for gmp, you do not need any compatibility headers, so it would be
acceptable to simply write

#ifdef __cplusplus
namespace gmp {
  extern "C" {

  /* GMP C declarations. */

#endif /* __cplusplus */
  } // extern "C"
} //namespace gmp

That is, if you want to add a namespace gmp. -- If there is a GNU standard
library, the namespace should perhaps be gnu or something instead.

If you include the C header <stddef.h>, which then under C++ should include
<cstddef>, before the above write

#ifdef __cplusplus
#include <cstddef>
#include <stddef.h>

That should do it.

  Hans Aberg

reply via email to

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