[Rcpp-devel] Extending Rcpp with a dummy class

Alessandro Mammana mammana at molgen.mpg.de
Thu Feb 13 15:55:36 CET 2014

Dear all,
I have another one of my stupid questions.
Suppose that for any reason I want to define my own vector class like this:

template<typename T>
struct Vec {
    T* ptr;
    int len;

so, without owning memory but just pointing to some (hopefully valid)
area of memory.
How can I extend Rcpp with this class so that I can have R vectors
automatically converted to these vectors?
The obvious answer is "read "extending Rcpp" instead of bothering us",
and I did it, and I tried the following:

#include <RcppCommon.h>

template<typename T>
struct Vec {
    T* ptr;
    int len;

    Vec(SEXP x){
        if (TYPEOF(x) != Rcpp::traits::r_sexptype_traits<T>::rtype)
            Rcpp::stop("incompatible types");

        Rcpp::Vector< Rcpp::traits::r_sexptype_traits<T>::rtype > rcpp_vec(x);
        ptr = rcpp_vec.begin();
        len = rcpp_vec.length();

#include <Rcpp.h>


but the compiler complains and says:
error: 'Vector' is not a member of 'Rcpp'

this is probably because I still did not import Rcpp.h, but in
"extending Rcpp" they say to include Rcpp.h only after defining my
classes, so how should I do?

Thanks in advance!

Alessandro Mammana, PhD Student
Max Planck Institute for Molecular Genetics
Ihnestraße 63-73
D-14195 Berlin, Germany

More information about the Rcpp-devel mailing list