[Rd] C/C++ namespaces

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Jul 1 15:55:38 CEST 2005

On Fri, 1 Jul 2005, Oleg Sklyar wrote:

> Dear community,
> this is just a suggestion, but might be useful for the following R
> releases.

It might be useful for future suggestions if you read the relevant manual 
before posting.  The developers are way ahead of you on this one.

> I was programming some C code for R and my compiler constantly showed me
> some crazy messages that a stdc++ macro length() was supplied with 4
> arguments whereas only one was required. The problem could be partially
> resolved by changing the order of includes. However as soon as the file
> was used from another units, the problem reappeared. Finally, it turned
> out that length() with one argument was a macro in Rinternals.h along
> with LENGTH. So I have to use namespace specifications on the rest of my
> code, because R - length() is not protected under a namespace.
> The problem is however that length is a pretty common name and as any
> other very common name should be protected by a C namespace to avoid
> confusion.

There is no such thing as a `C namespace': the word does not appear in the 
C99 standard.

> Therefore, the suggestion/question - wouldn't it be
> reasonable just to put the whole R includes into a unique namespace? It
> wouldn't require any modifications in the existing libraries because if
> the code is not conflicting, namespace can be omitted.

> Does it have sense?

No, as R is written in C not C++.  This issue only occurs if

1) you include Rinternals.h (which was designed for C programmers), and
2) do not take notice of the warning in the `Writing R Extensions' manual
and do not define R_NO_REMAP as it suggests.

Using C++ namespaces would be of no help for C extensions to R, and we 
have already provided a solution, in a back-compatible way for both C 
and C++ programmers.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-devel mailing list