[Rd] Problem with Rboolean in c++ code

Jan van der Laan rhelp at eoos.dds.nl
Fri May 24 08:53:15 CEST 2013

I am trying to use R_RegisterCFinalizerEx to ensure some c++ object is  
properly deleted after use. However, I run into some problems when  
trying to pass in the third argument which is an Rboolean.  The line  
'R_RegisterCFinalizerEx(p, finalizer, TRUE);' generates the following  
error when trying to compile using R CMD SHLIB:

g++ -I/usr/share/R/include -DNDEBUG      -fpic  -O2 -pipe -g  -c  
rboolean.cpp -o rboolean.o
rboolean.cpp: In function ?SEXPREC* create(SEXP)?:
rboolean.cpp:21:46: error: invalid conversion from ?int? to ?Rboolean?  
In file included from /usr/share/R/include/Rdefines.h:29:0,
                  from rboolean.cpp:4:
/usr/share/R/include/Rinternals.h:764:6: error:   initializing  
argument 3 of ?void R_RegisterCFinalizerEx(SEXP, R_CFinalizer_t,  
Rboolean)? [-fpermissive]
make: *** [rboolean.o] Error 1

I have managed to reduce the problem to the example below:

===== foo.cpp =====
#include <R.h>
#include <Rdefines.h>

void foo() {
   Rboolean b = TRUE;

With the extension .cpp this generates the error above, with the  
extension .c (don't put both in the same directory; at least not with  
the same name) it compiles without errors or warnings.

When looking at the headers (R_ext/Boolean.h and Rdefines.h) it seems  
that TRUE and FALSE are also defined as constants which seems to  
conflict with the enum values of Rboolean.

For now I use the compiler flag -fpermissive (as suggested by g++),  
but I suppose this is not acceptable when submitting to CRAN. Am I  
doing something wrong? Is there a workaround/solution?



More information about the R-devel mailing list