Open pepijn-devries opened 1 year ago
I find it useful as is - under the hood it means that cpp11 doesn't make new expressions for R to manage unless (explicitly) asked to. For what it's worth, an empty vector in cpp11 can be initialised with:
cpp11::writable::doubles x(0);
// x <- numeric(0)
Thank you for the quick response and clarification. However, your code won't compile on my machine, letting me know that call to constructor of writable::doubles (aka as 'r_vector') is ambiguous
... I had to explicitly cast the int
to R_xlen_t
like so:
cpp11::writable::doubles x((R_xlen_t)0);
After this I was indeed able to set an attribute value;
Maybe some attention to the initialisation of vectors could be given in one of the vignettes?
Ah I forgot that zero can be used to initialise other types and that leads to ambiguity. Definitely something that should be documented.
Thanks for your work on
cpp11
!Please consider the following. In c++ the code below will produce the error:
attempt to set an attribute on NULL
This would make sense when
x
actually is aNULL
, since in R the following code will produce the same error:However, I would expect the c++ code to behave as the following R code:
which works fine.
My question is: Is this expected behaviour from
cpp11
? If so what would be the recommended strategy to set an attribute to an empty vector (i.e., a vector of a defined type but length zero) in c++ usingcpp11
. If this is not expected behaviour, can it be fixed?My session looks like this: