[Rcpp-devel] Understanding the behaviour of const CharacterVector as a function parameter
Simon Zehnder
szehnder at uni-bonn.de
Sun Sep 29 14:42:49 CEST 2013
Hi Dirk,
thanks for the quick response!
I do not yet understand your comment towards the const correctness in regard to SEXP, but I will take a closer look at the doxygen of Rcpp.
New Version of Rcpp: Cool! Thanks for the tip, as I am curious now, I take the Vienna CRAN. The changes from Romain are the ones discussed lately on the rcpp-devel list regarding the shallow copy I assume.
Enjoy your day with your family!
Best
Simon
On Sep 29, 2013, at 2:16 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
>
> On 29 September 2013 at 14:06, Simon Zehnder wrote:
> | Dear Rcpp::Users and Rcpp::Devels,
> |
> | I would like to understand a certain behaviour of my code I encountered lately.
> |
> | I am working with CharacterVector and the following behaviour occurred:
> |
> | void test1 (Rcpp::CharacterVector &charv)
> | {
> | Rprintf("test1: %s\n", (char*) charv(0));
> | }
> |
> | void test2 (const Rcpp::CharacterVector &str)
> | {
> | Rprintf("test2: %s\n", (char*) charv(0));
> | }
> |
> | Using a string like "2013-05-04 20:23:21" for the Rcpp::CharacterVector gives the following outputs:
> |
> | test1: 2013-05-04 20:23:21
> |
> | test2: `
> |
> | This does also not change if I use a cast to const char* in test2. I tried something similar with strings and printing the c_str() of them, there the 'const' keyword does not make a difference - it always prints the correct string.
> |
> | Is this something specific to the Rcpp::CharacterVector, that uses a string_proxy for its elements returned by the operator ()? Is there a way to use const Rcpp::CharacterVector and get the behaviour of test1?
>
> Looks like a bug. (But note that const correctness of types build around SEXP
> is at best a promise -- we cannot undo the fact the the SEXP _is_ a pointer.)
>
> But hold on for a day til Rcpp 0.10.5 reaches your mirror, or grab it from
> CRAN in Vienna. It brings a lot of excellent changes, among them some fine
> work by Romain dealing with exactly that. Full announcement coming later once
> I am back from running and kid's soccer game.
>
> Dirk
>
> --
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
More information about the Rcpp-devel
mailing list