Sappiamo che la chiamata Rf_error()
deve essere evitata in Rcpp in quanto implica un longjmp su distruttori C++ sullo stack. Questo è il motivo per cui preferiamo lanciare eccezioni C++ nel codice Rcpp (come throw Rcpp::exception("...")
o tramite la funzione stop("...")
).Come generare un avviso R in modo sicuro in Rcpp
Tuttavia, gli avvisi R possono anche comportare una chiamata a Rf_error()
(questo comportamento dipende dall'opzione warn
). Quindi, una chiamata allo Rf_warning()
è anche rischiosa.
Rcpp::sourceCpp(code = '
#include <Rcpp.h>
using namespace Rcpp;
class Test {
public:
Test() { Rcout << "start\\n"; }
~Test() { Rcout << "end\\n"; }
};
// [[Rcpp::export]]
void test() {
Test t;
Rf_warning("test");
}
')
options(warn=10)
test()
## start
## Error in test() : (converted from warning) test
Vediamo che il distruttore non è stato chiamato (non c'è un messaggio di "fine").
Come generare un avviso R in un C++ - modo distruttore-friendly?
Penso che questo dovrebbe andare bene, basta fare attenzione agli oggetti statici. –