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

Jiqiang Guo guojq28 at gmail.com
Mon May 28 03:45:48 CEST 2012


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.

--
Jiqiang




library(Rcpp)library(inline)

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

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

print(sessionInfo())

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

fx(list(v = "hello"))


----

> library(Rcpp)
> library(inline)
>
> inc <- '
+ #include <Rcpp.h>
+ void fun(const Rcpp::List& lst) {
+ std::string s = Rcpp::as<std::string>(lst["v"]);
+ // std::string s = Rcpp::as<std::string>(lst[0]);
+ Rprintf(\"s=%s.\\n\", s.c_str());
+ }
+ '
>
> src <- '
+ Rcpp::List l(a);
+ fun(l);
+ '
>
> print(sessionInfo())
R version 2.15.0 (2012-03-30)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] inline_0.3.8 Rcpp_0.9.10.5
>
> fx <- cxxfunction(signature(a="list"), body = src,
+ includes = inc,
+ plugin = "Rcpp", verbose = TRUE)
>> setting environment variables:
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

>> LinkingTo : Rcpp
CLINK_CPPFLAGS =
-I"/home/jq/R/x86_64-pc-linux-gnu-library/2.15/Rcpp/include"

>> Program source :

1 :
2 : // includes from the plugin
3 :
4 : #include <Rcpp.h>
5 :
6 :
7 : #ifndef BEGIN_RCPP
8 : #define BEGIN_RCPP
9 : #endif
10 :
11 : #ifndef END_RCPP
12 : #define END_RCPP
13 : #endif
14 :
15 : using namespace Rcpp;
16 :
17 :
18 : // user includes
19 :
20 : #include <Rcpp.h>
21 : void fun(const Rcpp::List& lst) {
22 : std::string s = Rcpp::as<std::string>(lst["v"]);
23 : // std::string s = Rcpp::as<std::string>(lst[0]);
24 : Rprintf("s=%s.\n", s.c_str());
25 : }
26 :
27 :
28 : // declarations
29 : extern "C" {
30 : SEXP file56574b92b680( SEXP a) ;
31 : }
32 :
33 : // definition
34 :
35 : SEXP file56574b92b680( SEXP a ){
36 : BEGIN_RCPP
37 :
38 : Rcpp::List l(a);
39 : fun(l);
40 :
41 : END_RCPP
42 : }
43 :
44 :
Compilation argument:
/usr/lib/R/bin/R CMD SHLIB file56574b92b680.cpp 2>
file56574b92b680.cpp.err.txt
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
file56574b92b680.cpp: In function ‘void fun(const List&)’:
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]
make: *** [file56574b92b680.o] Error 1

ERROR(s) during compilation: source code errors or compiler configuration
errors!

Program source:
1:
2: // includes from the plugin
3:
4: #include <Rcpp.h>
5:
6:
7: #ifndef BEGIN_RCPP
8: #define BEGIN_RCPP
9: #endif
10:
11: #ifndef END_RCPP
12: #define END_RCPP
13: #endif
14:
15: using namespace Rcpp;
16:
17:
18: // user includes
19:
20: #include <Rcpp.h>
21: void fun(const Rcpp::List& lst) {
22: std::string s = Rcpp::as<std::string>(lst["v"]);
23: // std::string s = Rcpp::as<std::string>(lst[0]);
24: Rprintf("s=%s.\n", s.c_str());
25: }
26:
27:
28: // declarations
29: extern "C" {
30: SEXP file56574b92b680( SEXP a) ;
31: }
32:
33: // definition
34:
35: SEXP file56574b92b680( SEXP a ){
36: BEGIN_RCPP
37:
38: Rcpp::List l(a);
39: fun(l);
40:
41: END_RCPP
42: }
43:
44:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! file56574b92b680.cpp:
In function ‘void fun(const List&)’:
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]
make: *** [file56574b92b680.o] Error 1
Calls: cxxfunction -> compileCode
Execution halted
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20120527/fa7e325f/attachment-0001.html>


More information about the Rcpp-devel mailing list