This blog does not recommend it: http://blog.kalmbachnet.de/?postid=78
But I want to do it anyway. I suppose I need to wrap my Debug.Assert with some sort of #if
or #ifdef
or something like that. Also, does anyone have a good example of a Debug.Assert in C++ CLI
?
Suppose I have the following variable: String^ validationError = bldError.ToString();
And now I wish to do something like:
#if (DEBUG)
Debug.Assert(false, "Got the following validation error:" + validationError);
#endif
How can I do it safely in C++ CLI
, and are there additional gotchas to check for?
EDIT: Based on the answers, I have come up with the following macro:
#ifdef _DEBUG
#define CLIASSERT(condition, ...) System::Diagnostics::Debug:开发者_StackOverflow:Assert(condition, ##__VA_ARGS__)
#else
#define CLIASSERT(condition, ...) // This macro will completely evaporate in Release.
#endif
And here is an example of usage:
String^ strValidationError = bldError.ToString();
CLIASSERT(false, L"Schema validation error: " + strValidationError);
The blog post is accurate. Make it look like this:
#ifdef _DEBUG
Debug::Assert(...);
#endif
Pick one
#ifndef NDEBUG // or
#if !defined(NDEBUG) // or
#ifdef _DEBUG // or
#if defined(_DEBUG)
AFAIK NDEBUG
is defined in the ISO C++ standard (also used to enable/disable the standard library's assert
macro), whereas _DEBUG
is Microsoft-specific.
(Of course C++/CLI is not ISO C++, but in this case it doesn't make a difference)
MS has documented this behaviour at http://msdn.microsoft.com/en-us/library/system.diagnostics.conditionalattribute.aspx -- put briefly, when you do a release build, a compiler may or may not disable Assert and other such methods marked with ConditionalAttribute. For example the C# compiler does disable them, C++/CLI compiler does not. Even though the blog post is fairly old, I'd find it unlikely that the situation would change, considering what it says at that MSDN page.
精彩评论