Here is the code:
这是代码:
#include <Rcpp.h>
#include <iostream>
#include <assert.h>
#include <stdio.h>
using namespace Rcpp;
// [[Rcpp::export]]
double eudist(NumericVector x, NumericVector y) {
int nx = x.size();
int ny = y.size();
std::cout << nx << '\n' << ny << std::endl;
assert(nx == ny);
double dist=0;
for(int i = 0; i < nx; i++) {
dist += pow(x[i] - y[i], 2);
}
return sqrt(dist);
}
After sourcing it into R, I get the following result, apparently it does not abort when there is an error:
在将其获取到R之后,我得到以下结果,显然在出现错误时它不会中止:
#////////////////////////////////////////////////////
sourceCpp('x.cpp')
#////////////////////////////////////////////////////
eudist(c(0, 0), c(1, 1))
2
2
[1] 1.4142
#////////////////////////////////////////////////////
eudist(c(0, 0), c(1, 1, 1))
2
3
[1] 1.4142
3 个解决方案
#1
5
Note that assert()
etc are explicitly prohibited for CRAN uploads. Quoting from the CRAN Repo Policy page:
请注意,对于CRAN上载,明确禁止使用assert()等。从CRAN Repo Policy页面引用:
The code and examples provided in a package should never do anything which might be regarded as malicious or anti-social. The following are illustrative examples from past experience.
软件包中提供的代码和示例绝不应该做任何可能被视为恶意或反社交的事情。以下是过去经验的说明性示例。
- Compiled code should never terminate the R process within which it is running. Thus C/C++ calls to
assert
/abort
/exit
, Fortran calls toSTOP
and so on must be avoided. Nor may R code callq()
.编译代码永远不应该终止运行它的R进程。因此,C / C ++调用assert / abort / exit,Fortran调用STOP等等必须避免。 R代码也不能调用q()。
So the answer about debug mode is technically correct, but also note that you are not supposed to use this if you plan to upload CRAN at some point.
所以关于调试模式的答案在技术上是正确的,但是请注意,如果您计划在某个时刻上传CRAN,则不应该使用此方法。
#2
4
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.
使用throw而不是assert解决了这个问题,Rcpp实际上会把东西放在一个合适的try-catch块中,这是非常好的。
#include <Rcpp.h>
#include <iostream>
#include <assert.h>
#include <stdio.h>
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);
}
#3
0
For g++ use -g to enable debug options:
对于g ++,使用-g启用调试选项:
g++ -g code.cpp
Or debug mode in Visual Studio.
或Visual Studio中的调试模式。
#1
5
Note that assert()
etc are explicitly prohibited for CRAN uploads. Quoting from the CRAN Repo Policy page:
请注意,对于CRAN上载,明确禁止使用assert()等。从CRAN Repo Policy页面引用:
The code and examples provided in a package should never do anything which might be regarded as malicious or anti-social. The following are illustrative examples from past experience.
软件包中提供的代码和示例绝不应该做任何可能被视为恶意或反社交的事情。以下是过去经验的说明性示例。
- Compiled code should never terminate the R process within which it is running. Thus C/C++ calls to
assert
/abort
/exit
, Fortran calls toSTOP
and so on must be avoided. Nor may R code callq()
.编译代码永远不应该终止运行它的R进程。因此,C / C ++调用assert / abort / exit,Fortran调用STOP等等必须避免。 R代码也不能调用q()。
So the answer about debug mode is technically correct, but also note that you are not supposed to use this if you plan to upload CRAN at some point.
所以关于调试模式的答案在技术上是正确的,但是请注意,如果您计划在某个时刻上传CRAN,则不应该使用此方法。
#2
4
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.
使用throw而不是assert解决了这个问题,Rcpp实际上会把东西放在一个合适的try-catch块中,这是非常好的。
#include <Rcpp.h>
#include <iostream>
#include <assert.h>
#include <stdio.h>
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);
}
#3
0
For g++ use -g to enable debug options:
对于g ++,使用-g启用调试选项:
g++ -g code.cpp
Or debug mode in Visual Studio.
或Visual Studio中的调试模式。