On 8 May 2010 at 11:32, Douglas Bates wrote: | On Sat, May 8, 2010 at 10:31 AM, Dirk Eddelbuettel <[email protected]> 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.
Nice one. I had one quick try making nrow() and ncol() call a new sibbling functions _nrow() and _ncol() that I declared private and non-const, but the compiler saw through that. That additional layer gets taken away. The trouble with the stateful dimension vars d_nrow and d_ncol is course that one day you will forget to update them... I think in my defensive programming mood, I'd still stick with the nrow() and ncol() you had and I'd clinch my teeth and remove the const qualifier. | 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. I still haven't read much more than a section here or there. I quite like it, but it is loooong. -- Regards, Dirk _______________________________________________ Rcpp-devel mailing list [email protected] https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
