[Rcpp-devel] Rcpp::Dimension initialized from an SEXP

Dirk Eddelbuettel edd at debian.org
Sat May 8 17:31:25 CEST 2010


On 8 May 2010 at 09:21, Douglas Bates wrote:
| The enclosed section of code using Rcpp::Dimension fails to compile
| because of the const qualifiers for the simple::nrow and simple::ncol
| method functions.  If you omit those const qualifiers then it will
| compile and behave as desired.  Of course, I would prefer to have
| those method functions use the const qualifier.  Does anyone have
| suggestions on how to modify this code so I can use the const
| qualifiers?
| 
| My guess is that this is related to the Rcpp::Dimension::operator[]()
| returning an int& and not an int so somehow there is a delayed
| evaluation going on - but I haven't learned enough C++ to be able to
| decide exactly where the problem originates.

My guess is that it is the underlying SEXP (and here we put the stress on
P for pointer). My half-informed guess is that the compiler simply cannot see
how something passed as a pointer could be guaranteed const

A simple 'proof' of my conjecture ss that the following snippet compiles
fine:

-----------------------------------------------------------------------------
library(inline)
cdef <- '
class simple {
    Rcpp::Dimension ddOld;
    int dd[2];
public:
    simple(SEXP xp) : ddOld(xp) {}
    int nrow() const { return dd[0]; }
    int ncol() const { return dd[1]; }
};
'
cpp <- '
simple ss(ia);
return Rcpp::wrap(ss.nrow());
'
ff <- cfunction(signature(ia = "integer"), cpp, Rcpp = TRUE, includes = cdef)
-----------------------------------------------------------------------------

Here I left the SEXP alone, but renamed it. The accessor functions you are
concerned with here simply access an atomic vector, so that the straight int
elements are concerned.

So in short: I don't quite see how you could keep the const qualifiers on
that 'simple' class. But I may be missing something too...

Dirk
 
| ----------------------------------------------------------------------
| library(inline)
| cdef <- '
| class simple {
|     Rcpp::Dimension dd;
| public:
|     simple(SEXP xp) : dd(xp) {}
|     int nrow() const { return dd[0]; }
|     int ncol() const { return dd[1]; }
| };
| '
| cpp <- '
| simple ss(ia);
| return Rcpp::wrap(ss.nrow());
| '
| ff <- cfunction(signature(ia = "integer"), cpp, Rcpp = TRUE, includes = cdef)
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-- 
  Regards, Dirk


More information about the Rcpp-devel mailing list