title: How to generate an R warning safely in Rcpp
body: <p>We know that calling <code>Rf_error()</code> should be avoided in Rcpp as it involves a longjmp over C++ destructors on the stack. This is why we rather throw C++ exceptions in Rcpp code (like <code>throw Rcpp::exception("...")</code> or via the <code>stop("...")</code> function).</p>
  
  <p>However, R warnings may also result in a call to <code>Rf_error()</code> (this behavior depends on the <code>warn</code> option). So, a call to <code>Rf_warning()</code> is also risky.</p>
  
  <pre><code>Rcpp::sourceCpp(code = '
  
     #include &lt;Rcpp.h&gt;
     using namespace Rcpp;
  
     class Test {
        public:
           Test() { Rcout &lt;&lt; "start\\n"; }
           ~Test() { Rcout &lt;&lt; "end\\n"; }
     };
  
     // [[Rcpp::export]]
     void test() {
        Test t;
        Rf_warning("test");
     }
  ')
  
  options(warn=10)
  test()
  ## start
  ## Error in test() : (converted from warning) test
  </code></pre>
  
  <p>We see that the destructor hasn't been called (there's no "end" message).</p>
  
  <p>How to generate an R warning in a C++-destructor-friendly way?</p>