{"id":1220,"date":"2011-04-15T20:38:57","date_gmt":"2011-04-16T01:38:57","guid":{"rendered":"http:\/\/bitpost.com\/news\/?p=1220"},"modified":"2011-04-15T20:46:53","modified_gmt":"2011-04-16T01:46:53","slug":"redefine-your-assert-macro-so-they-dont-abort","status":"publish","type":"post","link":"https:\/\/bitpost.com\/news\/2011\/redefine-your-assert-macro-so-they-dont-abort\/","title":{"rendered":"Redefine your assert macros so they don&#8217;t abort"},"content":{"rendered":"<p>I love my <a href=\"http:\/\/en.wikipedia.org\/wiki\/Assertion_(computing)\">asserts<\/a>.  I use them like crazy throughout my code and catch errors faster than you and your whole testing department.  :>  The computer does exactly what I ASK of it, which sometimes is not the same as what I EXPECT of it.  So I wrap up my expectations with assert macros so my computer friend and I stay on the same page.  Happy happy joy joy.<\/p>\n<p>In Qt, we have the Q_ASSERT macro.  Qt default behavior is to abort on any Q_ASSERT macro failure.  This is weird to me (even if it is common).  Almost universally, I want to SEE what&#8217;s going on when I hit an assert.  Sometimes the assertion turns out to be wrong and may need adjusting.  Even if it&#8217;s right, sometimes it&#8217;s helpful to check the effect of the failed assertion.  And certainly, being able to walk back up the stack trace is critical to determine where things went wrong.  I can kill the program easily if I WANT to, but I may want to continue &#8211; it should be my choice.  It&#8217;s a no-brainer!<\/p>\n<p>So I don&#8217;t use Q_ASSERT.  Here&#8217;s my cross-platform C++ assert macro hackery.  It&#8217;s not perfect but it&#8217;s getting me by so far&#8230;<!--more--><\/p>\n<pre><code>\/\/-------------------------------------------------------------------\/\/\r\n\/\/\tAssertHelpers\r\n\/\/\r\n\/\/\tThis module attempts to provide ASSERT(), VERIFY(), and TRACE()\r\n\/\/ functionality under a wide range of builds and platforms.\r\n\/\/\r\n\/\/ ASSERT( test ) will break and notify if (test != true) in debug builds.\r\n\/\/ It will evaporate away under release builds.\r\n\r\n\/\/ VERIFY( test ) will always execute <test> but will only break\r\n\/\/ and notify the user if (test != true) under debug builds.\r\n\/\/\r\n\/\/\tTRACE( szFormat, ... ) will output to whatever std error output is\r\n\/\/\tavailable.\r\n\/\/ \r\n\/\/\tCopyright \u00a9 2011 A better Software.\r\n\/\/-------------------------------------------------------------------\/\/\r\n\r\n#ifndef ASSERT_HELPERS_H\r\n#define ASSERT_HELPERS_H\r\n\r\n#include \"PragmaMessages.h\"\t\t\t\/\/ For convenience, often used together.\r\n\r\n#ifndef ASSERT\r\n\r\n\t#ifdef _WIN32\r\n\r\n\t\t#ifdef false\t\/\/ what was wrong with ASSERT() on windows?  Q_WS_WIN\r\n\r\n            #define ASSERT(test) \r\n\t\t\t#define TRACE(test)\r\n\r\n\t\t#else\r\n\r\n\t\t\t\/\/ Use _ASSERT()\r\n\t\t\t#ifndef _INC_CRTDBG\r\n\t\t\t\t#include <crtdbg.h>\r\n\t\t\t#endif \/\/ _INC_CRTDBG\r\n\r\n\t\t\t#define ASSERT(test) _ASSERT(test)\r\n\t\t#endif\r\n\r\n    #else\r\n\r\n\t\t\/\/ QT for X11, Mac OS X, Embedded Linux\r\n        #if defined(Q_WS_X11) || defined(Q_WS_MAC) || defined(Q_WS_QWS)\r\n\t\t\r\n            \/\/ Sadly, Q_ASSERT kills off the app!  doh.\r\n            \/\/ #define ASSERT(test) Q_ASSERT(test)\r\n            \/\/ #define ASSERT(test) if (!(test)) {DebugBreak();}\r\n            # define ASSERT(x) ((x) ? (void)0 : qWarning(\"ASSERT: \\\"%s\\\" in %s (%d)\",#x,__FILE__,__LINE__))\r\n            #define TRACE(test)\r\n\r\n\t\t#endif\r\n\r\n\t#endif\r\n\r\n#endif \/\/ ASSERT\r\n\r\n#ifndef VERIFY\r\n\r\n\t#ifdef _WIN32\r\n\r\n\t\t#ifdef _DEBUG\r\n\t\t\t#define VERIFY(test) ASSERT(test)\r\n\t\t#else\r\n\t\t\r\n\t\t\t\/\/ This pragma prevents warnings about \"(test) == true;\" type statements under release mode.\r\n\t\t\t#pragma warning(disable : 4553 )\r\n\t\t\r\n\t\t\t#define VERIFY(test) test\r\n\r\n\t\t#endif \/\/ _DEBUG\r\n\r\n\t#endif \/\/ _WIN32\r\n\r\n#endif \/\/ VERIFY\r\n\r\n#ifndef TRACE\r\n\r\n\t#ifdef _WIN32\r\n\r\n           #ifndef _INC_CRTDBG\r\n\t\t#include <crtdbg.h>\r\n\t   #endif \/\/ _INC_CRTDBG\r\n\r\n   \t   #ifdef _DEBUG\r\n\t\t   #define TRACE(test) \t_CrtDbgReport( _CRT_WARN, NULL, NULL, NULL, test )\r\n\t   #else\r\n\t\t   #define TRACE(test)\r\n\t   #endif\r\n\r\n        #endif \/\/ _WIN32\r\n\r\n#endif \/\/ TRACE\r\n\r\n#endif\t\/\/ ASSERT_HELPERS_H<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I love my asserts. I use them like crazy throughout my code and catch errors faster than you and your whole testing department. :> The computer does exactly what I ASK of it, which sometimes is not the same as what I EXPECT of it. So I wrap up my expectations with assert macros so [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[22],"tags":[117,26],"class_list":["post-1220","post","type-post","status-publish","format-standard","hentry","category-cpp","tag-assert","tag-debugging"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9M11L-jG","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/1220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/comments?post=1220"}],"version-history":[{"count":4,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/1220\/revisions"}],"predecessor-version":[{"id":1222,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/1220\/revisions\/1222"}],"wp:attachment":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/media?parent=1220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/categories?post=1220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/tags?post=1220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}