cpp11 icon indicating copy to clipboard operation
cpp11 copied to clipboard

Idiomatic way to cast or convert between different r_vector types

Open vspinu opened this issue 3 years ago • 0 comments
trafficstars

The following conversions results in a run time error Invalid input type, expected 'integer' actual 'logical'

// cpp11::logicals val
cpp11::integers ints(val);
cpp11::integers ints = cpp11::as_cpp<cpp11::integers>(val);

It's not clear to me why logicals cannot be re-interpreted as integers. After all it's the same internal representation.

What is an idiomatic way to convert within cpp11 when converting at R level is not an option? Is it enough to cpp11::integers X2(Rf_coerceVector(X, INTSXP));, or do I still need to protect the converted vector?

It would be great if this information could be added to the conversion section of the vignette.

Thanks! `

vspinu avatar Feb 18 '22 16:02 vspinu