hi.  the Rcpp-modules document, section 2.2.2, mentions that one can
write a routine to disambiguate between different constructors taking
the same number of arguments.

i wonder, in the case one has *not* provided such a disambiguator, would
it be possible to offer up an error message, maybe at compile time?
(and point the programmer at the relevant document.)

attached is a test case which compiles, but gets a runtime error   it
took me a bit of head scratching, compiling Rcpp with debug symbols so i
could follow the action, etc.

(in *this* case, the specific error message is: "Error: not an S4
object".)

cheers, Greg
----
// the question here is: how to return an R list of reference objects

// to run, first put the source in inc
// (inc <- <SINGLEQUOTE><SOURCE><SINGLEQUOTE>)
// then do something like:

#if 0

require(inline);
require(Rcpp);

setClass("box", representation(
  ne="integer"));

setMethod(
  "initialize",
  signature("box"),
  definition=function(.Object, ne) {
    .Object@ne <- ne;
    return(.Object);
  });

cld <- cxxfunction(,plugin="Rcpp", includes=inc);

clm <- Module("test5_module", getDynLib(cld));
space2d <- clm$space2d;

new(space2d, new("box", 1L))   # this will work
new(space2d, matrix(0, nrow=4, ncol=4))  # this will fail

#endif /* 0 */

#include <R.h>
#include <RcppCommon.h>
#include <Rcpp/S4.h>

class box {
public:
    int ne;
public:
    box() {}
    box(const int _ne) {
        ne = _ne;
    }
    box(const box& _box) {
        ne = _box.ne;
    }
};

#if defined(RcppCommon_h)
namespace Rcpp {
    template <> inline SEXP wrap(const box& box) {
        S4 ret("box");
        ret.slot("ne") = wrap(box.ne);
        return wrap(ret);
    }
    template <> inline box as(SEXP sexp) {
        box box;
        S4 s4(sexp);
        box.ne = s4.slot("ne");
        return box;
    }
}
#endif /* defined(RcppCommon_h) */


#include <Rcpp.h>

class space2d {
public:
    box global;                 // the box in global coordinates
public:
    space2d(box bbox, int initval = 0) {
        global = bbox;
    }
    space2d(Rcpp::IntegerMatrix matrix) {
        int
            minx = 0,
            maxx = matrix.ncol()-1,
            miny = 0,
            maxy = matrix.nrow()-1;
        global = box(minx*miny*maxx*maxy);
    }
};


RCPP_EXPOSED_CLASS(space2d)

using namespace Rcpp;
RCPP_MODULE(test5_module) {
    class_<space2d>("space2d")
        .constructor<box,int>()
        .constructor<box>()
        .constructor<Rcpp::IntegerMatrix>();
}
_______________________________________________
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to