[Rcpp-devel] Problem exposing constructor taking std::vector of user defined class to R using Rcpp-attributes and Rcpp-modules

Luke.Domanski at csiro.au Luke.Domanski at csiro.au
Thu Aug 22 01:21:28 CEST 2013


Hi All,

I running into some problems using a combination of Rcpp-attributes and Rcpp-modules to expose a C++ class library, and would appreciate your help.

I am trying to expose to R the constructor of a C++ class template which accepts a std vector of the template parameter T as input, as shown in the following toy class A used to replicate the problem:

template <class T> class A {
    public:
        std::vector<T> vec_of_T;

        A( std::vector<T> in_vec );
};

The class and constructor will only be exposed to R as a specific instantiation of the class A with template parameter T=class B, i.e. A<B>, where B is implemented in both C++ and R, and the C++ implementation of B has a constructor B::B(SEXP):

#include <RcppCommon.h>

class B {
    public:
        int id;

        B (SEXP b);
        B (int id=-1);
};

/*** R
# Reference Class of B
B_R <- setRefClass( "B_R",
    fields = list(id="numeric")
)
*/

template <class T> class A {
    public:
        std::vector<T> vec_of_T;

        A( std::vector<T> in_vec );
};

#include <Rcpp.h>

B::B(SEXP b){
    Rcpp::Reference in_b(b);
    id=in_b.field("id");
}

B::B(int id){
    this->id=id;
}

The idea is to allow an R vector of Reference Class objects B_R to be passed to the C++ class A<B> constructor. To achieve this I have used the following Rcpp-modules directly after the preceding code:

using namespace Rcpp;

RCPP_MODULE(testing) {

    class_< A<B> >("A")

    .constructor< std::vector<B> >()
    ;
}

I then use Rcpp-attributed sourceCpp() to compile the code from the R prompt, and generate the R bindings to the C++ class A<B>.

> sourceCpp(file="test.cpp", verbose=TRUE)

>From my understand of  section 3.2 in Rcpp-introduction.pdf, and what I could glean about the handling of RCPP_MODULE declarations within Rcpp-attribute (e.g. help(sourceCpp)), the conversion of "STL vectors of [...] arbitrary types that offer a constructor that takes a SEXP" should be supported by Rcpp, and Rcpp-attributes should automatically generate wrappers/bindings which utilize them.

However, when I run the above sourceCpp command I get the following error which is stretching my understanding on both C++ and Rcpp:


C:/PROGRA~1/R/R-30~1.1/bin/i386/R CMD SHLIB -o "sourceCpp_42052.dll" "test.cpp"
g++ -m32 -I"C:/PROGRA~1/R/R-30~1.1/include" -DNDEBUG     -I"C:/Users/me/Documents/R/win-library/3.0/Rcpp/include"  -I"d:/RCompile/CRANpkg/extralibs64/local/include"     -O2 -Wall  -mtune=core2 -c test.cpp -o test.o
In file included from C:/Users/me/Documents/R/win-library/3.0/Rcpp/include/RcppCommon.h:110:0,
                 from test.cpp:1:
C:/Users/me/Documents/R/win-library/3.0/Rcpp/include/Rcpp/internal/export.h: In function 'void Rcpp::internal::export_range(SEXP, InputIterator) [with InputIterator = __gnu_cxx::__normal_iterator<B*, std::vector<B> >, SEXP = SEXPREC*]':
C:/Users/me/Documents/R/win-library/3.0/Rcpp/include/Rcpp/internal/Exporter.h:47:17:   instantiated from 'T Rcpp::traits::RangeExporter<T>::get() [with T = std::vector<B>]'
C:/Users/me/Documents/R/win-library/3.0/Rcpp/include/Rcpp/as.h:83:33:   instantiated from 'T Rcpp::internal::as(SEXP, Rcpp::traits::r_type_generic_tag) [with T = std::vector<B>, SEXP = SEXPREC*]'
C:/Users/me/Documents/R/win-library/3.0/Rcpp/include/Rcpp/as.h:126:89:   instantiated from 'T Rcpp::as(SEXP) [with T = std::vector<B>, SEXP = SEXPREC*]'
C:/Users/me/Documents/R/win-library/3.0/Rcpp/include/Rcpp/module/Module_generated_Constructor.h:47:43:   instantiated from 'Class* Rcpp::Constructor_1<Class, U0>::get_new(SEXPREC**, int) [with Class = A<B>, U0 = std::vector<B>, SEXP = SEXPREC*]'
test.cpp:59:1:   instantiated from here
C:/Users/me/Documents/R/win-library/3.0/Rcpp/include/Rcpp/internal/export.h:90:4: error: no matching function for call to 'export_range__dispatch(SEXPREC*&, __gnu_cxx::__normal_iterator<B*, std::vector<B> >&, Rcpp::traits::r_type_traits<B>::r_category)'
C:/Users/me/Documents/R/win-library/3.0/Rcpp/include/Rcpp/internal/export.h:90:4: note: candidates are:
C:/Users/me/Documents/R/win-library/3.0/Rcpp/include/Rcpp/internal/export.h:71:14: note: template<class InputIterator, class value_type> void Rcpp::internal::export_range__dispatch(SEXP, InputIterator, Rcpp::traits::r_type_primitive_tag)
C:/Users/me/Documents/R/win-library/3.0/Rcpp/include/Rcpp/internal/export.h:80:8: note: template<class InputIterator, class value_type> void Rcpp::internal::export_range__dispatch(SEXP, InputIterator, Rcpp::traits::r_type_string_tag)
make: *** [test.o] Error 1
Error in sourceCpp(file = "test.cpp", verbose = TRUE) :
  Error 1 occurred building shared library.

Am I missing something in my code?

Thanks in advance,
Luke Domanski.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130821/962540ec/attachment.html>


More information about the Rcpp-devel mailing list