Sometimes you need to help the compiler.
The converter Rcpp::as<T>()
converts from R, the converter Rcpp::wrap()
converts to R.
R> cppFunction('std::string hello(CharacterVector vec) {
return as<std::string>(vec[0]); }')
R> hello(c("The", "quick", "brown", "fox"))
[1] "The"
R>
This is all documented, and there are copious examples. It may well be possible to find smarter template programming to automate this conversion too, and if so we'd love to see a patch.
For the second part of your question: these come from R, and does not know or have std::string
. We have lightweight wrapper classes which generally avoid copying. Hence Rcpp::CharacterVector
-- which you can convert to std::vector<std::string>
.
Edit late 2016: Note that we can also do
R> cppFunction("std::string f(CharacterVector x,int i) {return std::string(x[i]);}")
R> foo(c("The", "quick", "brown", "fox"), 1)
[1] "quick"
R>