Dear List:<div><br></div><div>I am having problem with getting Rcpp::List's element by name when </div><div>Rcpp::List is const reference. </div><div><br></div><div>Here is a simple demo and output.  Am I not supposed to do that, but </div>
<div>it is fine using index of numbers. </div><div><br></div><div>--</div><div>Jiqiang </div><div><br></div><div><br></div><div><br></div><div><br><pre style="white-space:pre-wrap;background-color:rgb(255,255,255);font-size:medium">
<span class="PreProc" style="color:rgb(255,64,255)">library</span><span class="Special" style="color:rgb(255,64,255)">(</span>Rcpp<span class="Special" style="color:rgb(255,64,255)">)</span>
<span class="PreProc" style="color:rgb(255,64,255)">library</span><span class="Special" style="color:rgb(255,64,255)">(</span>inline<span class="Special" style="color:rgb(255,64,255)">)</span>

inc <span class="Statement" style="color:rgb(255,255,0)"><-</span> <span class="Constant" style="color:rgb(255,96,96)">'</span>
<span class="Constant" style="color:rgb(255,96,96)">#include <Rcpp.h></span>
<span class="Constant" style="color:rgb(255,96,96)">void fun(const Rcpp::List& lst) {</span>
<span class="Constant" style="color:rgb(255,96,96)">  std::string s = Rcpp::as<std::string>(lst["v"]);  // problematic</span>
<span class="Constant" style="color:rgb(255,96,96)">  // std::string s = Rcpp::as<std::string>(lst[0]); // fine</span>
<span class="Constant" style="color:rgb(255,96,96)">  Rprintf(</span><span class="Special" style="color:rgb(255,64,255)">\"</span><span class="Constant" style="color:rgb(255,96,96)">s=%s.</span><span class="Special" style="color:rgb(255,64,255)">\\</span><span class="Constant" style="color:rgb(255,96,96)">n</span><span class="Special" style="color:rgb(255,64,255)">\"</span><span class="Constant" style="color:rgb(255,96,96)">, s.c_str()); </span>
<span class="Constant" style="color:rgb(255,96,96)">} </span>
<span class="Constant" style="color:rgb(255,96,96)">'</span>

src <span class="Statement" style="color:rgb(255,255,0)"><-</span> <span class="Constant" style="color:rgb(255,96,96)">'</span>
<span class="Constant" style="color:rgb(255,96,96)">Rcpp::List l(a); </span>
<span class="Constant" style="color:rgb(255,96,96)">fun(l); </span>
<span class="Constant" style="color:rgb(255,96,96)">'</span>

print<span class="Special" style="color:rgb(255,64,255)">(</span>sessionInfo<span class="Special" style="color:rgb(255,64,255)">())</span>

fx <span class="Statement" style="color:rgb(255,255,0)"><-</span> cxxfunction<span class="Special" style="color:rgb(255,64,255)">(</span>signature<span class="Special" style="color:rgb(255,64,255)">(</span>a <span class="Statement" style="color:rgb(255,255,0)">= </span><span class="Constant" style="color:rgb(255,96,96)">"list"</span><span class="Special" style="color:rgb(255,64,255)">)</span><span class="Special" style="color:rgb(255,64,255)">,</span> body <span class="Statement" style="color:rgb(255,255,0)">=</span> src<span class="Special" style="color:rgb(255,64,255)">,</span>
                  includes <span class="Statement" style="color:rgb(255,255,0)">=</span> inc<span class="Special" style="color:rgb(255,64,255)">,</span>
                  plugin <span class="Statement" style="color:rgb(255,255,0)">=</span> <span class="Constant" style="color:rgb(255,96,96)">"Rcpp"</span><span class="Special" style="color:rgb(255,64,255)">,</span> verbose <span class="Statement" style="color:rgb(255,255,0)">=</span> <span class="Constant" style="color:rgb(255,96,96)">TRUE</span><span class="Special" style="color:rgb(255,64,255)">)</span>

fx<span class="Special" style="color:rgb(255,64,255)">(</span><span class="Type" style="color:rgb(0,255,0)">list</span><span class="Special" style="color:rgb(255,64,255)">(</span>v <span class="Statement" style="color:rgb(255,255,0)">= </span><span class="Constant" style="color:rgb(255,96,96)">"hello"</span><span class="Special" style="color:rgb(255,64,255)">))</span></pre>
<pre style="white-space:pre-wrap;background-color:rgb(255,255,255);font-size:medium"><span class="Special" style="color:rgb(255,64,255)"><br></span></pre><font face="courier new, monospace">----<br><br>> library(Rcpp)<br>
> library(inline)<br>> <br>> inc <- '<br>+ #include <Rcpp.h><br>+ void fun(const Rcpp::List& lst) {<br>+   std::string s = Rcpp::as<std::string>(lst["v"]); <br>+   // std::string s = Rcpp::as<std::string>(lst[0]); <br>
+   Rprintf(\"s=%s.\\n\", s.c_str()); <br>+ } <br>+ ' <br>> <br>> src <- '<br>+ Rcpp::List l(a); <br>+ fun(l); <br>+ '<br>> <br>> print(sessionInfo())<br>R version 2.15.0 (2012-03-30)<br>
Platform: x86_64-pc-linux-gnu (64-bit)<br><br>locale:<br> [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              <br> [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    <br> [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   <br>
 [7] LC_PAPER=C                 LC_NAME=C                 <br> [9] LC_ADDRESS=C               LC_TELEPHONE=C            <br>[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       <br><br>attached base packages:<br>[1] stats     graphics  grDevices utils     datasets  methods   base     <br>
<br>other attached packages:<br>[1] inline_0.3.8  Rcpp_0.9.10.5<br>> <br>> fx <- cxxfunction(signature(a="list"), body = src, <br>+                   includes = inc,<br>+                   plugin = "Rcpp", verbose = TRUE) <br>
 >> setting environment variables: <br>PKG_LIBS =  -L/home/jq/R/x86_64-pc-linux-gnu-library/2.15/Rcpp/lib -lRcpp -Wl,-rpath,/home/jq/R/x86_64-pc-linux-gnu-library/2.15/Rcpp/lib<br><br> >> LinkingTo : Rcpp<br>CLINK_CPPFLAGS =  -I"/home/jq/R/x86_64-pc-linux-gnu-library/2.15/Rcpp/include" <br>
<br> >> Program source :<br><br>   1 : <br>   2 : // includes from the plugin<br>   3 : <br>   4 : #include <Rcpp.h><br>   5 : <br>   6 : <br>   7 : #ifndef BEGIN_RCPP<br>   8 : #define BEGIN_RCPP<br>   9 : #endif<br>
  10 : <br>  11 : #ifndef END_RCPP<br>  12 : #define END_RCPP<br>  13 : #endif<br>  14 : <br>  15 : using namespace Rcpp;<br>  16 : <br>  17 : <br>  18 : // user includes<br>  19 : <br>  20 : #include <Rcpp.h><br>  21 : void fun(const Rcpp::List& lst) {<br>
  22 :   std::string s = Rcpp::as<std::string>(lst["v"]); <br>  23 :   // std::string s = Rcpp::as<std::string>(lst[0]); <br>  24 :   Rprintf("s=%s.\n", s.c_str()); <br>  25 : } <br>  26 : <br>
  27 : <br>  28 : // declarations<br>  29 : extern "C" {<br>  30 : SEXP file56574b92b680( SEXP a) ;<br>  31 : }<br>  32 : <br>  33 : // definition<br>  34 : <br>  35 : SEXP file56574b92b680( SEXP a ){<br>  36 : BEGIN_RCPP<br>
  37 : <br>  38 : Rcpp::List l(a); <br>  39 : fun(l); <br>  40 : <br>  41 : END_RCPP<br>  42 : }<br>  43 : <br>  44 : <br>Compilation argument:<br> /usr/lib/R/bin/R CMD SHLIB file56574b92b680.cpp 2> file56574b92b680.cpp.err.txt <br>
g++ -I/usr/share/R/include -DNDEBUG   -I"/home/jq/R/x86_64-pc-linux-gnu-library/2.15/Rcpp/include"   -fpic  -O3 -pipe  -g  -c file56574b92b680.cpp -o file56574b92b680.o<br>file56574b92b680.cpp: In function ‘void fun(const List&)’:<br>
file56574b92b680.cpp:22:48: error: passing ‘const List {aka const Rcpp::Vector<19>}’ as ‘this’ argument of ‘Rcpp::Vector<RTYPE>::NameProxy Rcpp::Vector<RTYPE>::operator[](const string&) [with int RTYPE = 19, Rcpp::Vector<RTYPE>::NameProxy = Rcpp::internal::generic_name_proxy<19>, std::string = std::basic_string<char>]’ discards qualifiers [-fpermissive]<br>
make: *** [file56574b92b680.o] Error 1<br><br>ERROR(s) during compilation: source code errors or compiler configuration errors!<br><br>Program source:<br>  1: <br>  2: // includes from the plugin<br>  3: <br>  4: #include <Rcpp.h><br>
  5: <br>  6: <br>  7: #ifndef BEGIN_RCPP<br>  8: #define BEGIN_RCPP<br>  9: #endif<br> 10: <br> 11: #ifndef END_RCPP<br> 12: #define END_RCPP<br> 13: #endif<br> 14: <br> 15: using namespace Rcpp;<br> 16: <br> 17: <br> 18: // user includes<br>
 19: <br> 20: #include <Rcpp.h><br> 21: void fun(const Rcpp::List& lst) {<br> 22:   std::string s = Rcpp::as<std::string>(lst["v"]); <br> 23:   // std::string s = Rcpp::as<std::string>(lst[0]); <br>
 24:   Rprintf("s=%s.\n", s.c_str()); <br> 25: } <br> 26: <br> 27: <br> 28: // declarations<br> 29: extern "C" {<br> 30: SEXP file56574b92b680( SEXP a) ;<br> 31: }<br> 32: <br> 33: // definition<br> 34: <br>
 35: SEXP file56574b92b680( SEXP a ){<br> 36: BEGIN_RCPP<br> 37: <br> 38: Rcpp::List l(a); <br> 39: fun(l); <br> 40: <br> 41: END_RCPP<br> 42: }<br> 43: <br> 44: <br>Error in compileCode(f, code, language = language, verbose = verbose) : <br>
  Compilation ERROR, function(s)/method(s) not created! file56574b92b680.cpp: In function ‘void fun(const List&)’:<br>file56574b92b680.cpp:22:48: error: passing ‘const List {aka const Rcpp::Vector<19>}’ as ‘this’ argument of ‘Rcpp::Vector<RTYPE>::NameProxy Rcpp::Vector<RTYPE>::operator[](const string&) [with int RTYPE = 19, Rcpp::Vector<RTYPE>::NameProxy = Rcpp::internal::generic_name_proxy<19>, std::string = std::basic_string<char>]’ discards qualifiers [-fpermissive]<br>
make: *** [file56574b92b680.o] Error 1<br>Calls: cxxfunction -> compileCode<br>Execution halted<br><br></font><pre style="white-space:pre-wrap;background-color:rgb(255,255,255);font-size:medium"><span class="Special" style="color:rgb(255,64,255)"><br>
</span></pre><pre style="white-space:pre-wrap;background-color:rgb(255,255,255);font-size:medium"><span class="Special" style="color:rgb(255,64,255)"><br></span></pre><pre style="white-space:pre-wrap;background-color:rgb(255,255,255);font-size:medium">
<span class="Special" style="color:rgb(255,64,255)"><br></span></pre></div>