[Rcpp-devel] problem with Rcpp::List's element by name when Rcpp::List is const reference

Dirk Eddelbuettel edd at debian.org
Mon May 28 04:56:02 CEST 2012


Hi Jiqiang,

On 27 May 2012 at 21:45, Jiqiang Guo wrote:
| Dear List:
| 
| I am having problem with getting Rcpp::List's element by name when 
| Rcpp::List is const reference. 
| 
| Here is a simple demo and output.  Am I not supposed to do that, but 
| it is fine using index of numbers. 

I think you simply cannot do 'const' --- if you remove the const qualifier
everything works just fine. Below is a shorter version of your example:


edd at max:~$ cat /tmp/jiqiang.r 
library(inline)

inc <- '
void fun(Rcpp::List & lst) {
  std::string s = Rcpp::as<std::string>(lst["v"]);
  Rprintf(\"s=%s.\\n\", s.c_str());
}
'

src <- '
  Rcpp::List l(a);
  fun(l);
'

fx <- cxxfunction(signature(a = "list"), body = src,
                  includes = inc, plugin = "Rcpp")

fx(list(v = "hello"))
edd at max:~$ r /tmp/jiqiang.r 
Loading required package: methods
s=hello.
edd at max:~$ 


If you want const correctness in interface, copy from our 'proxy objects'
(which reference the underlying SEXP objects from R) into proper C++ / STL
objects and move from there.

Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com  


More information about the Rcpp-devel mailing list