<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Feb 24, 2015 at 9:45 AM, Miratrix, Luke <span dir="ltr"><<a href="mailto:lmiratrix@fas.harvard.edu" target="_blank">lmiratrix@fas.harvard.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
As an inexperienced person myself, I was trying to wrap a C++ stand-alone<br>
package so it could be called from R and was trying to preserve some of<br>
the safety features and error-checking.  My understanding of asserts is<br>
they are to catch disasters that indicate bugs in the code itself, and are<br>
thus distinct from normal exceptions.  They therefore print out the failed<br>
check and a line-number in the source file and bail.<br>
<br>
When using Rcpp, I came up against CRANıs admonition to not print to<br>
stderr and so couldnıt use assert() and stay ³legal.²  But I found this<br>
out only after annoying people by not doing what I was supposed to do.<br>
Given that, having assert() redefined so it is compliant with CRAN and<br>
plays nice with R, but also maintains its behavior would be pretty cool, I<br>
think.<br>
<br>
All this being said, Nathan Kurzıs comments also seem good except I think<br>
it is ³illegal² to print to STDERR directly before aborting, since it is<br>
only aborting the C++ part, and not the entire R session.  Hence my hack<br>
of printing to a string buffer and then handing it to the Rf_error call.<br>
I donıt know the guts of Rf_error, but I had assumed it would copy the<br>
string to its own world before unwinding the stack.  If it doesnıt then I<br>
agree this is going to cause problems.  What is the correct way to pass a<br>
message up, then?<br></blockquote><div><br></div><div>If I recall, the other prohibition of CRAN is to never ever call abort (R owns the event loop) so assert-like behavior is not gonna fly.</div><div><br></div><div>THK</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
(Sorry for delay in posting this: the rcpp devel list does not get along<br>
with my mail client.)<br>
<br>
<br>
<br>
Sincerely,<br>
<br>
Luke Miratrix<br>
Assistant Professor of Statistics<br>
<br>
        Note: Due to my RSI (wrist trouble), e-mail often abrupt.<br>
<br>
<br>
--<br>
<br>
Department of Statistics<br>
Science Center<br>
<br>
Harvard University<br>
1 Oxford Street<br>
Cambridge MA 02138-2901<br>
<br>
<br>
<a href="mailto:lmiratrix@stat.harvard.edu">lmiratrix@stat.harvard.edu</a><br>
<a href="tel:510-735-7635" value="+15107357635">510-735-7635</a><br>
<br>
<br>
<br>
<br>
<br>
<br>
On 2/18/15, 3:25 PM, "Dale Smith" <<a href="mailto:DSmith@nexidia.com">DSmith@nexidia.com</a>> wrote:<br>
<br>
>I haven't been very active lately with Rcpp, but I like Nathan's advice<br>
>below. Don't redefine assert(), I think that's not friendly to<br>
>inexperienced people and will just generate more questions.<br>
><br>
>Dale Smith, Ph.D. | Data Scientist | nexidia | office: <a href="tel:%2B1%20404%20495%207220" value="+14044957220">+1 404 495 7220</a><br>
>ext 4008 | fax: <a href="tel:%2B1%20404%20495%207221" value="+14044957221">+1 404 495 7221</a>| <a href="http://nexidia.com" target="_blank">nexidia.com</a><br>
><br>
>-----Original Message-----<br>
>From: <a href="mailto:rcpp-devel-bounces@lists.r-forge.r-project.org">rcpp-devel-bounces@lists.r-forge.r-project.org</a><br>
>[mailto:<a href="mailto:rcpp-devel-bounces@lists.r-forge.r-project.org">rcpp-devel-bounces@lists.r-forge.r-project.org</a>] On Behalf Of<br>
>Nathan Kurz<br>
>Sent: Wednesday, February 18, 2015 3:19 PM<br>
>To: Miratrix, Luke<br>
>Cc: <a href="mailto:rcpp-devel@lists.r-forge.r-project.org">rcpp-devel@lists.r-forge.r-project.org</a><br>
>Subject: Re: [Rcpp-devel] assert() for Rcpp?<br>
><br>
>On Tue, Feb 17, 2015 at 4:41 PM, Miratrix, Luke<br>
><<a href="mailto:lmiratrix@fas.harvard.edu">lmiratrix@fas.harvard.edu</a>> wrote:<br>
>> The proposed code:<br>
>><br>
>> #include <stdio.h><br>
>><br>
>> #ifdef NDEBUG<br>
>> # define assert(EX)<br>
>> #else<br>
>> # define assert(EX) (void)((EX) || (__assert (#EX, __FILE__,<br>
>> __LINE__),0)) #endif<br>
>><br>
>> void __assert (const char *msg, const char *file, int line) {<br>
>>     char buffer [100];<br>
>>     snprintf( buffer, 100, "Assert Failure: %s at %s line #%d", msg,<br>
>> file, line );<br>
>>     ::Rf_error( buffer );<br>
>> }<br>
><br>
>Getting more people using assert-like macros seems like a great idea.<br>
>Weighing in as a C programmer with limited knowledge of Rcpp, I'd<br>
>suggest:<br>
><br>
>1) Don't redefine assert() or __assert().  You'll confuse people and it<br>
>will somehow manage to break things.  Instead, define your own macro with<br>
>a different name, likely one in all caps that starts with "R_" or "RCPP_".<br>
><br>
>2) Only keep the name 'assert' in the macro if you are keeping the<br>
>semantics of assert(), that is, it dies on failure if NDEBUG not defined.<br>
> If it prints a warning and recovers, or defaults to off, use<br>
>a different word.   I personally don't like the inverted NDEBUG<br>
>approach, so would suggest a different semantics and different word.<br>
><br>
>3) It's debatable if you want to allow it to be turned off or not.<br>
>If it can be turned off, people will misuse it to guard against errors<br>
>and be surprised when it doesn't.   I often use a noisy warning that<br>
>only runs when DEBUG is positively defined (DEBUG_WARN_IF) and an abort<br>
>that  cannot be turned off (ERROR_ABORT_IF).<br>
><br>
>4) Don't try to snprintf() to a local buffer and then return.   I<br>
>don't know C++ semantics for exceptions, but at least in C, combining<br>
>local stack variables with stack unwinding is asking for trouble.  If you<br>
>are aborting, print to STDERR directly.  If you are continuing, use a<br>
>normal heap allocation.<br>
><br>
>Nathan Kurz<br>
><a href="mailto:nate@verse.com">nate@verse.com</a><br>
>_______________________________________________<br>
>Rcpp-devel mailing list<br>
><a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
><a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
<br>
_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><a href="http://www.keittlab.org/" target="_blank">http://www.keittlab.org/</a></div></div>
</div></div>