Why does assert not work here?

后端 未结 3 1538
清歌不尽
清歌不尽 2021-01-05 17:04

Here is the code:

#include 
#include 
#include 
#include 

using namespace Rcpp;


// [[Rcpp::ex         


        
3条回答
  •  花落未央
    2021-01-05 17:55

    The problem is solved by using throw instead of assert, Rcpp will actually put things in a proper try-catch block, which is super nice.

    #include 
    #include 
    #include 
    #include 
    
    using namespace Rcpp;
    
    
    // [[Rcpp::export]]
    double eudist(NumericVector x, NumericVector y) {
        int nx = x.size();
        int ny = y.size();
        Rcout << nx << '\n' << ny << std::endl;
    
        if(nx != ny) {
            throw std::invalid_argument("Two vectors are not of the same length!");
        }
    
        double dist=0;
        for(int i = 0; i < nx; i++) {
            dist += pow(x[i] - y[i], 2);
        }
    
        return sqrt(dist);
    }
    

提交回复
热议问题