2

I'm trying out Rcpp / RcppArmadillo in RStudio with the following code:

#include <RcppArmadillo.h>

//[[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp;
using std::exp;
using std::log1p;

// [[Rcpp::export]]
arma::vec log1pexp(arma::vec x) {
  for(int ii = 0; ii < x.n_elem; ++ii){
    if(x(ii) < 18.0){
      x(ii) = log1p(exp(x(ii)));
    } else{
      x(ii) = x(ii) + exp(-x(ii));
    }
  }
  return x;
}

RStudio says the calls to exp are ambiguous. I've tried calling std::exp in the code instead of using std::exp but have no success. The code compiles without warnings through Rcpp::sourceCpp('filename.cpp'). If I cast (float)x(ii) in the code the warning disappears, but not if I cast (double)x(ii).

Any insight appreciated, I'm pretty inexperienced with both C++ and RStudio.

Picture of what's going on

enter image description here

7
  • When you say, "RStudio says", are you talking about the code diagnostics messages in the IDE? Or are you getting an actual compiler error?
    – nrussell
    Commented Dec 6, 2016 at 14:45
  • 1
    At any rate, I would advise against using an unqualified exp, as (based on the declarations in the above code) this could refer to either exp the std:: namespace or exp from the Rcpp:: namespace (and possibly the C function exp from <math.h>).
    – nrussell
    Commented Dec 6, 2016 at 14:49
  • Thanks @nrussell. The code compiles without errors. It's the IDE that gives this diagnostic message.
    – KOE
    Commented Dec 6, 2016 at 14:56
  • I can't reproduce this, which version of RStudio are you using?
    – nrussell
    Commented Dec 6, 2016 at 15:12
  • 1
    You should probably just write up the answer and mark it accepted, since you have all of the information. Just make a note of the RStudio version you upgraded to in your answer.
    – nrussell
    Commented Dec 6, 2016 at 15:23

1 Answer 1

4

For starters, don't do

using namespace Rcpp;
using std::exp;
using std::log1p;

If in doubt, be explicit. Your code then becomes

#include <RcppArmadillo.h>

// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::plugins(cpp11)]]

// [[Rcpp::export]]
arma::vec log1pexp(arma::vec x) {
    for(size_t ii = 0; ii < x.n_elem; ++ii){
        if(x(ii) < 18.0){
            x(ii) = std::log1p(std::exp(x(ii)));
        } else{
            x(ii) = x(ii) + std::exp(-x(ii));
        }
    }
    return x;
}

and compiled without a hitch (after I also changed int to size_t for the loop) -- and without an issue in the RStudio IDE (using a fairly recent daily, 1.0.116).

There are

  • std::exp() in the standard library, using double
  • Rcpp::exp() from Rcpp Sugar, using our vectors
  • arma::exp() from Armadillo using its vectors

and I always found it easiest to be explicit.

Edit: I had missed log1p. Prefixing it with std:: also requires C++11. Two changes made.

2
  • Thanks for accepting. Re-reading, I think a case may be made that it was an IDE bug. You did declare std::exp via the using, yet got warned. Oh well. My "better safe than sorry" advice still holds. Commented Dec 6, 2016 at 16:31
  • 2
    Yep, it's actually turned up again even in the new IDE version. If I copy-paste your code there, RStudio says use of exp is ambiguous again. I'll just follow your advice and be explicit, and ignore the RStudio message.
    – KOE
    Commented Dec 6, 2016 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.