[Rcpp-devel] "expecting a string" and not finding it in a CharacterVector

Dirk Eddelbuettel edd at debian.org
Sat Feb 9 14:06:20 CET 2013

On 8 February 2013 at 20:58, Davor Cubranic wrote:
| That fixed just the runtime fault in "as", right? Or did it somehow also take care of ctor for const string from an element of const CharacterVector?

The former:

R> sourceCpp("/tmp/davor.cpp")
R> cv <- c("The", "quick", "brown", "fox")
R> getString1(cv, 2)
[1] "brown"
R> getString2(cv, 2)
[1] "brown"


#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
std::string getString1(CharacterVector cv, int pos) {
  char *c = cv[pos];
  std::string s(c);
  return s;

// [[Rcpp::export]]
std::string getString2(CharacterVector cv, int pos) {
  std::string s(cv[pos]);
  return s;

Variants with 'const CharacterVector' still fail. That would involve a lot
more work to extend as<> to permit them.   And at the end of the day the SEXP
is always a pointer into R's data so I am not really sure we really can
assure const correctness all the way.


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

More information about the Rcpp-devel mailing list