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

Douglas Bates bates at stat.wisc.edu
Sat May 8 18:32:02 CEST 2010


On Sat, May 8, 2010 at 10:31 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
>
> 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

Yes, I was concentrating on the fact that I was returning a value, not
a reference or a pointer, from the method function.  I think what you
are saying is that for the compiler once it sees that I am doing
something with the SEXP within the method function it figures that all
bets are off as far as the const keyword goes.

I did get around the problem by the simple expedient of defining int
members, d_nrow and d_ncol, assigning their values during construction
and returning those integer values in the const member functions.

By the way, I have for a couple of weeks now been reading the C++
Annotations book that you mentioned and find it very interesting.  I
am trying to follow some of the suggestions for class design from that
book, which is why there is a d_ prefix on the private data member
names.


> 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