[Rcpp-devel] Problem exposing inherited member function of derived class to R through RCPP_MODULE

Luke.Domanski at csiro.au Luke.Domanski at csiro.au
Wed Sep 25 06:30:33 CEST 2013


Hi All,

Thanks for your previous help on http://comments.gmane.org/gmane.comp.lang.r.rcpp/5972

I have been able to implement an RCPP_MODULE of the "real code" containing classes represented by classes A and B in the above thread. That set/group of classes implements a generic base class that will be extended from to provide particular functionality.

I now wish to expose via RCPP_MODULE a class A_derived which derives from A<B_derived>. I want to expose member variable var1 and function fun1 of class A_derived which are both inherited from A<B_derived>. i.e. no overwriting definition of A_derived::var1 or A_derived::fun1 exist, only A<B_derived>::var1 and A<B_derived>::fun1.

However, the following RCPP_MODULE definition produces the subsequent compiler error.

RCPP_MODULE(testing) {
    class_<A_derived>("A_derived")
    .field( "var1", &A_derived::var1)
    .method("fun1", &A_derived::fun1)
    ;
}

test2.cpp: In function 'void _rcpp_module_testing_init()':
test2.cpp:33:37: error: no matching function for call to 'Rcpp::class_<A_derived>::field(const char [5], double A<B_derived>::*)'
test2.cpp:33:37: note: candidate is:
C:/.../R/win-library/3.0/Rcpp/include/Rcpp/module/Module_Field.h:68:7: note: template<class T> Rcpp::class_<Class>::self& Rcpp::class_::field(const char*, T Class::*, const char*) [with T = T, Class = A_derived, Rcpp::class_<Class>::self = Rcpp::class_<A_derived>]

It seems the compiler is converting A_derived::var1 and fun1 into their true references A<B_derived>::var1 and fun1, but the Rcpp templates cannot handle the situation where the referenced variable or function is not a "proper" member of the class.

However, the error goes away if overwrite var1 and fun1 in A_derived, and implement A_derived::fun1() as:

void A_derived::fun1() {
    A<B_derived>::fun1();
};

This is not ideal.
Can someone please help me formulate the correct RCPP_MODULE definition? Is there something that I am doing wrong, or have I once again triggered a chain of the template meta program that is not implemented in Rcpp.

Full code and error reduct follows:

-----test2.cpp-----
#include <RcppCommon.h>

class B {
    public:
        B (SEXP b);
};

template <class T> class A {
    public:
        double var1;
        void fun1();
};

class B_derived : public B {
    public:
        B_derived(SEXP b);
};
class A_derived : public A<B_derived> {};

#include <Rcpp.h>

B::B(SEXP b) {};

template <class T> void A<T>::fun1(){
    Rcpp::Rcout << "Object of Type: " << typeid(this).name() << "\n";
}

B_derived::B_derived(SEXP b) : B::B(b) {};

using namespace Rcpp;

RCPP_MODULE(testing) {
    class_<A_derived>("A_derived")
    .field( "var1", &A_derived::var1)
    .method("fun1", &A_derived::fun1)
    ;
}
----end test2.cpp----

> require(R)
> sourceCpp(file="test2.cpp", verbose=TRUE)
...snip...
g++ -m32 -I"C:/PROGRA~1/R/R-30~1.1/include" -DNDEBUG     -I"C:/.../R/win-library/3.0/Rcpp/include"  -I"d:/RCompile/CRANpkg/extralibs64/local/include"     -O2 -Wall  -mtune=core2 -c test2.cpp -o test2.o
test2.cpp: In function 'void _rcpp_module_testing_init()':
test2.cpp:33:37: error: no matching function for call to 'Rcpp::class_<A_derived>::field(const char [5], double A<B_derived>::*)'
test2.cpp:33:37: note: candidate is:
C:/.../R/win-library/3.0/Rcpp/include/Rcpp/module/Module_Field.h:68:7: note: template<class T> Rcpp::class_<Class>::self& Rcpp::class_::field(const char*, T Class::*, const char*) [with T = T, Class = A_derived, Rcpp::class_<Class>::self = Rcpp::class_<A_derived>]
make: *** [test2.o] Error 1
Error in sourceCpp(file = "test2.cpp", verbose = TRUE) :
  Error 1 occurred building shared library.


Cheers,
Luke Domanski.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130925/ab15b7a6/attachment-0001.html>


More information about the Rcpp-devel mailing list