Why Q_ASSERT instead of statement

Qt has a Q_ASSERT macro. What is the advantage of using this instead of assert from <cassert> ?

+7
assert assertions qt
source share
2 answers

Q_ASSERT is a custom assert macro that supposedly enhances the standard assert function.

The error message is handled by qFatal() , which may improve slightly on some platforms than the standard assert macro. For example, on Windows, it will launch the Visual Studio debugger at the point where this statement fails, rather than just calling abort() .

You can also redirect the output of Qt error reporting functions, such as qFatal , to your custom message handler ( qInstallMessageHandler () ). This can be useful, for example, if you want to redirect the error message to a file.

Also note that Q_ASSERT disabled by the Q_ASSERT macro (while assert disabled by NDEBUG ): this can be used to separate your statements between Qt code and the rest.

+12
source share

From the same document:

It does nothing if QT_NO_DEBUG was defined at compile time.

So think about it: Q_ASSERT is independent of the absence of NDEBUG , because assert is. #ifndef NDEBUG is required for assert() to do something, and is also often used to demarcate other common debugging properties only in the user code (and possibly in the library ..?).

Using a separate macro is an advantage for those who want to debug only Qt related things without leaving NDEBUG undefined and thus weighing the rest of the code with debugging materials that inflate if NDEBUG not defined, especially assert() s.

So, you can compile with -DNDEBUG , but not -DQT_NO_DEBUG if you want to compile β€œnormal” things using release mode semantics, but at the same time apply debugging to Qt materials.

This is very useful when developing a complex GUI application. I do not use Qt (yet?), But I see the advantage of using such things in my selected GTK + / gtkmm tools [... which, I am sure, exist, but I have not looked at them yet ;-)]

I recall a recent animated discussion about this here, intertwined with a discussion of an orthogonal proposal: ISO C ++ Standard - Future Suggestions> Trace Information for the Exception Stack.

+3
source share

All Articles