<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Stupid C++ Tricks: Adventures in Assert</title>
	<atom:link href="http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/feed/" rel="self" type="application/rss+xml" />
	<link>http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/</link>
	<description>programming is hard.</description>
	<lastBuildDate>Sun, 28 Feb 2010 21:59:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: GameCoder.it &#8722; L&#8217;assert, questa sconosciuta</title>
		<link>http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/comment-page-1/#comment-77</link>
		<dc:creator>GameCoder.it &#8722; L&#8217;assert, questa sconosciuta</dc:creator>
		<pubDate>Sun, 28 Feb 2010 21:59:27 +0000</pubDate>
		<guid isPermaLink="false">http://cnicholson.net/?p=3#comment-77</guid>
		<description>[...] articolo di JP sulle nuove assert statiche del C++0x. riferimenti: http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/ http://msinilo.pl/blog/?p=212   Share and [...]</description>
		<content:encoded><![CDATA[<p>[...] articolo di JP sulle nuove assert statiche del C++0x. riferimenti: <a href="http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/" rel="nofollow">http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/</a> <a href="http://msinilo.pl/blog/?p=212" rel="nofollow">http://msinilo.pl/blog/?p=212</a>   Share and [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jan Řrřola Kadlec</title>
		<link>http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/comment-page-1/#comment-74</link>
		<dc:creator>Jan Řrřola Kadlec</dc:creator>
		<pubDate>Tue, 12 Jan 2010 14:30:28 +0000</pubDate>
		<guid isPermaLink="false">http://cnicholson.net/?p=3#comment-74</guid>
		<description>Thanks everyone for the article and comments. This is how my C/C++ assert macros look like now:

#include 

typedef int (*AssertHandler)(char const*, char const*, int);

int default_assert_handler(char const* expr, char const* file, int line)
{ fprintf(stderr, &quot;Assertion (%s) failed in %s (%d)\n&quot;, expr, file, line);
  return 1;
}

AssertHandler assert_handler = default_assert_handler;

#if defined(_MSC_VER)
  #define ASSERT_HALT() __debugbreak()
#else
  #include 
  #define ASSERT_HALT() exit(__LINE__)
#endif

#ifdef DEBUG
  #define assert(x) ((void)(!(x) &amp;&amp; assert_handler(#x, __FILE__, __LINE__) &amp;&amp; (ASSERT_HALT(), 1)))
#else
  #define assert(x) ((void)sizeof(x))
#endif</description>
		<content:encoded><![CDATA[<p>Thanks everyone for the article and comments. This is how my C/C++ assert macros look like now:</p>
<p>#include </p>
<p>typedef int (*AssertHandler)(char const*, char const*, int);</p>
<p>int default_assert_handler(char const* expr, char const* file, int line)<br />
{ fprintf(stderr, &#8220;Assertion (%s) failed in %s (%d)\n&#8221;, expr, file, line);<br />
  return 1;<br />
}</p>
<p>AssertHandler assert_handler = default_assert_handler;</p>
<p>#if defined(_MSC_VER)<br />
  #define ASSERT_HALT() __debugbreak()<br />
#else<br />
  #include<br />
  #define ASSERT_HALT() exit(__LINE__)<br />
#endif</p>
<p>#ifdef DEBUG<br />
  #define assert(x) ((void)(!(x) &amp;&amp; assert_handler(#x, __FILE__, __LINE__) &amp;&amp; (ASSERT_HALT(), 1)))<br />
#else<br />
  #define assert(x) ((void)sizeof(x))<br />
#endif</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin Wentzell</title>
		<link>http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/comment-page-1/#comment-73</link>
		<dc:creator>Kevin Wentzell</dc:creator>
		<pubDate>Sat, 02 Jan 2010 07:00:36 +0000</pubDate>
		<guid isPermaLink="false">http://cnicholson.net/?p=3#comment-73</guid>
		<description>THANK YOU SO MUCH! I was in the middle of banging my head down to a few thousand lines of code when I stumbled upon your page. The problem:

#define ASSERT( n )\
        if ( !n )\
        {\
            return;\
        }

and eventually got a hint when this code wasn&#039;t returning from the block:

ASSERT( 2 &gt; 2 )

Thank you &gt;.&lt;</description>
		<content:encoded><![CDATA[<p>THANK YOU SO MUCH! I was in the middle of banging my head down to a few thousand lines of code when I stumbled upon your page. The problem:</p>
<p>#define ASSERT( n )\<br />
        if ( !n )\<br />
        {\<br />
            return;\<br />
        }</p>
<p>and eventually got a hint when this code wasn&#8217;t returning from the block:</p>
<p>ASSERT( 2 &gt; 2 )</p>
<p>Thank you &gt;.&lt;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/comment-page-1/#comment-69</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Thu, 17 Dec 2009 23:35:40 +0000</pubDate>
		<guid isPermaLink="false">http://cnicholson.net/?p=3#comment-69</guid>
		<description>Just a note that (at least under the version I&#039;m using) __builtin_trap() in GCC works like __debugbreak().  I&#039;m finding this real handy with Xilinx&#039;s GDB-based debugger for the PowerPC405 on their Virtex-4 FPGAs.  Thanks!</description>
		<content:encoded><![CDATA[<p>Just a note that (at least under the version I&#8217;m using) __builtin_trap() in GCC works like __debugbreak().  I&#8217;m finding this real handy with Xilinx&#8217;s GDB-based debugger for the PowerPC405 on their Virtex-4 FPGAs.  Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anthony Whitaker</title>
		<link>http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/comment-page-1/#comment-66</link>
		<dc:creator>Anthony Whitaker</dc:creator>
		<pubDate>Sat, 21 Nov 2009 00:30:33 +0000</pubDate>
		<guid isPermaLink="false">http://cnicholson.net/?p=3#comment-66</guid>
		<description>I&#039;ve found that the code below does a decent job on several compilers at silencing that pesky constant conditional expression warning.

// This is simply not enough on some compilers.
do {} while(__LINE__ == -1)

Instead I use these macros to surround multi-line macros.
#define GorkMacroOpen do {
#define GorkMacroClose } while(__LINE__ == -1, false)

Looking forward to more articles Charles :)  
It&#039;s time to update</description>
		<content:encoded><![CDATA[<p>I&#8217;ve found that the code below does a decent job on several compilers at silencing that pesky constant conditional expression warning.</p>
<p>// This is simply not enough on some compilers.<br />
do {} while(__LINE__ == -1)</p>
<p>Instead I use these macros to surround multi-line macros.<br />
#define GorkMacroOpen do {<br />
#define GorkMacroClose } while(__LINE__ == -1, false)</p>
<p>Looking forward to more articles Charles <img src='http://cnicholson.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
It&#8217;s time to update</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cyberursus</title>
		<link>http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/comment-page-1/#comment-63</link>
		<dc:creator>cyberursus</dc:creator>
		<pubDate>Sun, 04 Oct 2009 16:43:31 +0000</pubDate>
		<guid isPermaLink="false">http://cnicholson.net/?p=3#comment-63</guid>
		<description>The following code

#define NDEBUG 1

#include 

int main()
{
	bool failed = false;
	assert(failed);
}

shows &#039;unused variable&#039; warning for both GCC and MSVC...</description>
		<content:encoded><![CDATA[<p>The following code</p>
<p>#define NDEBUG 1</p>
<p>#include </p>
<p>int main()<br />
{<br />
	bool failed = false;<br />
	assert(failed);<br />
}</p>
<p>shows &#8216;unused variable&#8217; warning for both GCC and MSVC&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: charles</title>
		<link>http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/comment-page-1/#comment-61</link>
		<dc:creator>charles</dc:creator>
		<pubDate>Wed, 16 Sep 2009 19:53:56 +0000</pubDate>
		<guid isPermaLink="false">http://cnicholson.net/?p=3#comment-61</guid>
		<description>1.  There is no such thing as the &quot;C/C++ STL&quot;.  The STL is the Standard Template Library, and it&#039;s a set of generic and robust containers and algorithms for C++.  It is not available for C, because it is built heavily upon C++ templates.  You happen to be using one implementation of the STL from one specific vendor that may or may not publish an assert macro.  I suspect you&#039;re confused, intended to write &quot;The C++ Standard Library&quot; instead of &quot;C/C++ STL&quot;,  and are really using the one in the cassert system header.  I&#039;m not sure, however, because you didn&#039;t post a complete program anywhere.

2.  Using signals is significantly less useful than having your own macro, because your signal handler doesn&#039;t have any callsite information that you can report to the user.  __LINE__, __FILE__, and __FUNCTION__ will only tell you that you&#039;re in your signal handler, and they won&#039;t tell you where the assert happened.  You absolutely need to write your own macro if you want to use a custom error handler AND know about the callsite.

I guess my answer to your final question &quot;Why see all that trouble&quot; is simply &quot;Because there&#039;s no easier way to do it.&quot;  You&#039;ve presented an inferior solution, and if you&#039;re happy with it, then by all means use it.
</description>
		<content:encoded><![CDATA[<p>1.  There is no such thing as the &#8220;C/C++ STL&#8221;.  The STL is the Standard Template Library, and it&#8217;s a set of generic and robust containers and algorithms for C++.  It is not available for C, because it is built heavily upon C++ templates.  You happen to be using one implementation of the STL from one specific vendor that may or may not publish an assert macro.  I suspect you&#8217;re confused, intended to write &#8220;The C++ Standard Library&#8221; instead of &#8220;C/C++ STL&#8221;,  and are really using the one in the cassert system header.  I&#8217;m not sure, however, because you didn&#8217;t post a complete program anywhere.</p>
<p>2.  Using signals is significantly less useful than having your own macro, because your signal handler doesn&#8217;t have any callsite information that you can report to the user.  __LINE__, __FILE__, and __FUNCTION__ will only tell you that you&#8217;re in your signal handler, and they won&#8217;t tell you where the assert happened.  You absolutely need to write your own macro if you want to use a custom error handler AND know about the callsite.</p>
<p>I guess my answer to your final question &#8220;Why see all that trouble&#8221; is simply &#8220;Because there&#8217;s no easier way to do it.&#8221;  You&#8217;ve presented an inferior solution, and if you&#8217;re happy with it, then by all means use it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: justonstragler</title>
		<link>http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/comment-page-1/#comment-60</link>
		<dc:creator>justonstragler</dc:creator>
		<pubDate>Wed, 16 Sep 2009 16:05:32 +0000</pubDate>
		<guid isPermaLink="false">http://cnicholson.net/?p=3#comment-60</guid>
		<description>[quote][i]Not great.  These warnings only show up on MSVC’s (level 4) and gcc’s (-Wall) highest warning levels[/i][/quote]

I use the standard assert provided in C/C++ STL. I don&#039;t get that warning (or any other) even with &#039;-Wall&#039; in my compiler&#039;s paramerets. I use Dev-C++ 4.9.9.2, which is build on gcc and g++.

[quote][i]We can try the standard cast-to-void trick:
#define POW2_ASSERT(x) do { (void)(x); } while(0)[/i][/quote]

Standard C/C++ STL define&#039;s null version of assert as follows:
#define assert(x)	((void)0)

No tricks, no hassle, so why bother writing your own when STL contains equally good (or better?) solution? Or did you forget to mention something, Charles Nicholson?

Having your own handler for errors is not an excuse: you can use the signals.h (which is also provided in C/C++ STL) to set a custom handler for SIGABRT signal, which is raised when STL&#039;s assert() detects failure, and thus either default signal handler for that signal or your custom handler is called.

I did it with gcc and C/C++ STL, i get zero warnings wether or not NDEBUG is defined and i have my own, custom error handler (log, cleanup, shutdown).

So, my point is: Why see all that trouble?</description>
		<content:encoded><![CDATA[<p>[quote][i]Not great.  These warnings only show up on MSVC’s (level 4) and gcc’s (-Wall) highest warning levels[/i][/quote]</p>
<p>I use the standard assert provided in C/C++ STL. I don&#8217;t get that warning (or any other) even with &#8216;-Wall&#8217; in my compiler&#8217;s paramerets. I use Dev-C++ 4.9.9.2, which is build on gcc and g++.</p>
<p>[quote][i]We can try the standard cast-to-void trick:<br />
#define POW2_ASSERT(x) do { (void)(x); } while(0)[/i][/quote]</p>
<p>Standard C/C++ STL define&#8217;s null version of assert as follows:<br />
#define assert(x)	((void)0)</p>
<p>No tricks, no hassle, so why bother writing your own when STL contains equally good (or better?) solution? Or did you forget to mention something, Charles Nicholson?</p>
<p>Having your own handler for errors is not an excuse: you can use the signals.h (which is also provided in C/C++ STL) to set a custom handler for SIGABRT signal, which is raised when STL&#8217;s assert() detects failure, and thus either default signal handler for that signal or your custom handler is called.</p>
<p>I did it with gcc and C/C++ STL, i get zero warnings wether or not NDEBUG is defined and i have my own, custom error handler (log, cleanup, shutdown).</p>
<p>So, my point is: Why see all that trouble?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam Rosenfield</title>
		<link>http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/comment-page-1/#comment-50</link>
		<dc:creator>Adam Rosenfield</dc:creator>
		<pubDate>Mon, 08 Jun 2009 18:13:22 +0000</pubDate>
		<guid isPermaLink="false">http://cnicholson.net/?p=3#comment-50</guid>
		<description>One feature that&#039;s missing: you can&#039;t use an assert() as an expression.  Specifically, you can&#039;t use it in a ternary operator:

condition ? POW2_ASSERT(something) : somethingElse();

Granted, this isn&#039;t a terribly common or useful construct.  But it is the reason why the ANSI C assert() macro is specified to be an expression of type void.  You can implement this feature by replacing the if() in the definition of POW2_ASSERT() with a ?: and judicious use of parentheses and the comma operator.</description>
		<content:encoded><![CDATA[<p>One feature that&#8217;s missing: you can&#8217;t use an assert() as an expression.  Specifically, you can&#8217;t use it in a ternary operator:</p>
<p>condition ? POW2_ASSERT(something) : somethingElse();</p>
<p>Granted, this isn&#8217;t a terribly common or useful construct.  But it is the reason why the ANSI C assert() macro is specified to be an expression of type void.  You can implement this feature by replacing the if() in the definition of POW2_ASSERT() with a ?: and judicious use of parentheses and the comma operator.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steven Kah Hien Wong</title>
		<link>http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/comment-page-1/#comment-9</link>
		<dc:creator>Steven Kah Hien Wong</dc:creator>
		<pubDate>Wed, 11 Mar 2009 05:54:32 +0000</pubDate>
		<guid isPermaLink="false">http://cnicholson.net/?p=3#comment-9</guid>
		<description>Sad to hear about Pow2, but I was just searching for this awesome macro... I love it. :)</description>
		<content:encoded><![CDATA[<p>Sad to hear about Pow2, but I was just searching for this awesome macro&#8230; I love it. <img src='http://cnicholson.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>
