What is the safe way to call Debug.Assert from the C ++ CLI?

This blog does not recommend: http://blog.kalmbachnet.de/?postid=78

But I still want to do it. I suppose I need to wrap my Debug.Assert with some kind of #ifor #ifdefor something like this. Also, does anyone have a good Debug.Assert example in C++ CLI?

Suppose I have the following variable: String^ validationError = bldError.ToString(); And now I want to do something like:

#if (DEBUG)
Debug.Assert(false, "Got the following validation error:" + validationError);
#endif

How can I do this safely in C++ CLI, and are there any additional fixes to check?

EDIT: Based on the answers, I came up with the following macro:

#ifdef _DEBUG
#define CLIASSERT(condition, ...) System::Diagnostics::Debug::Assert(condition, ##__VA_ARGS__)
#else
#define CLIASSERT(condition, ...) // This macro will completely evaporate in Release.
#endif

And here is a usage example:

String^ strValidationError = bldError.ToString();
CLIASSERT(false, L"Schema validation error: " + strValidationError);
+3
source share
3

#ifndef NDEBUG // or
#if !defined(NDEBUG) // or

#ifdef _DEBUG // or
#if defined(_DEBUG)

AFAIK NDEBUG ISO ++ ( / assert), _DEBUG Microsoft.

(, ++/CLI - ISO ++, )

+3

. :

#ifdef _DEBUG
    Debug::Assert(...);
#endif
+5

MS recorded this behavior at http://msdn.microsoft.com/en-us/library/system.diagnostics.conditionalattribute.aspx - briefly leave it when you create the release assembly, the compiler may or may not disable Approval and other such methods, marked with a conditional attribute. For example, the C # compiler disables them; the C ++ / CLI compiler does not. Although the blog post is quite old, I would hardly change the situation given what it says on this MSDN page.

+2
source

Source: https://habr.com/ru/post/1767062/


All Articles