bra_racing
bra_racing

Reputation: 620

Rcpp function with no argument

I am developing a R package using Rcpp, and now I an trying to include the following c++ function without input arguments:

int printVariables() {
    // Code
    return 0;
}

I have in my RcppExports.cpp the next function:

int printVariables();
RcppExport SEXP printVariables() {
BEGIN_RCPP
    Rcpp::RObject __result;
    Rcpp::RNGScope __rngScope;
    __result = Rcpp::wrap(printVariables());
    return __result;
END_RCPP
}

Finally, in the RcppExports.R I have an entry as follows:

printVariables <- function() {
    .Call('printVariables', PACKAGE = 'my_package')
}

When I compile the code I get this error:

RcppExports.cpp: In function ‘SEXPREC* printVariables()’:
RcppExports.cpp:54:37: error: ambiguating new declaration of ‘SEXPREC* printVariables()’
 RcppExport SEXP printVariables() {
RcppExports.cpp:53:5: note: old declaration ‘int printVariables()’
 int printVariables();

Anyone knows how can I solve that issue?

Upvotes: 0

Views: 804

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368181

Just place an ignored argument there. Return type void works, but you must have at least a single argument you are free to ignore.

R> cppFunction("void foo(int ignored=0) { double d; }")
R> foo()
R> 

See the Rcpp Attributes vignette for all the glorious details.

Edit I do stand corrected, thanks to @hrbrmstr, who correctly points out that this does of course work in a package. I.e. even the sample function created by RStudio when asked to do File -> New Project -> New Directory -> R Package, and when 'Package w/ Rcpp' is select has this:

// [[Rcpp::export]]
List rcpp_hello() {
  CharacterVector x = CharacterVector::create("foo", "bar");
  NumericVector y   = NumericVector::create(0.0, 1.0);
  List z            = List::create(x, y);
  return z;
}

which is evidently sans argument.

Upvotes: 3

Related Questions