[Rcpp-devel] Idiom for accessing scalars

Dirk Eddelbuettel edd at debian.org
Sat Jan 7 19:11:42 CET 2012


On 7 January 2012 at 19:02, Romain François wrote:
| Not thinking about a rewrite, just adding e.g. this for int:
| 
| template <> inline int as<int>( SEXP x, 
| ::Rcpp::traits::r_type_primitive_tag ) {
|             return Rf_asInteger( x ) ;
|          }
| 
| so that we let R's asInteger do the actual work, and Doug is right, it 
| does a better job than the current emplated version.
| 
| For example this works:
| 
|  > fx <- cxxfunction( signature(x_="ANY"), ' int x = as<int>( x_ ); 
| return wrap(x); ', plugin="Rcpp")
|  >
|  > fx( "123" )
| [1] 123

Right. Specialisation makes sense as we just add features like the
char-to-int cast.
 
| However,
| 
|  > fx( c(1L, 3L ) )
| [1] 1
| 
| this passes, where as the current version complains that it should be a 
| single value.

Which is a very useful test to have. Hmpf.

Dirk

-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx


More information about the Rcpp-devel mailing list