<QtAssert> | Qt Core | Qt 6.10.1
Description: Macros for condition checks during development and debugging.

- Qt 6.10
- Qt Core
- <QtAssert>
- Qt 6.10.1 Reference Documentation

# <QtAssert>

Macros for condition checks during development and debugging. More...

Header: | `#include <QtAssert>`

- Deprecated members

## Functions

T * | q_check_ptr(T *p)

## Macros

void | Q_ASSERT(bool test)
void | Q_ASSERT_X(bool test, const char *where, const char *what)
void | Q_CHECK_PTR(void *pointer)
void | Q_UNREACHABLE()
`(since 6.5)` void | Q_UNREACHABLE_RETURN(...)

## Detailed Description

## Function Documentation

### template <typename T> T *q_check_ptr(T *p)

Uses Q_CHECK_PTR on p, then returns p.

This can be used as an inline version of Q_CHECK_PTR.

## Macro Documentation

### void Q_ASSERT(bool test)

Prints a warning message containing the source code file name and line number if test is `false`.

Q_ASSERT() is useful for testing pre- and post-conditions during development. It does nothing if `QT_NO_DEBUG` was defined during compilation.

Example:

     // File: div.cpp

     #include <QtGlobal>

     int divide(int a, int b)
     {
         Q_ASSERT(b != 0);
         return a / b;
     }

If `b` is zero, the Q_ASSERT statement will output the following message using the qFatal() function:

     ASSERT: "b != 0" in file div.cpp, line 7

See also Q_ASSERT_X(), qFatal(), and Debugging Techniques.

### void Q_ASSERT_X(bool test, const char *where, const char *what)

Prints the message what together with the location where, the source file name and line number if test is `false`.

Q_ASSERT_X is useful for testing pre- and post-conditions during development. It does nothing if `QT_NO_DEBUG` was defined during compilation.

Example:

     // File: div.cpp

     #include <QtGlobal>

     int divide(int a, int b)
     {
         Q_ASSERT_X(b != 0, "divide", "division by zero");
         return a / b;
     }

If `b` is zero, the Q_ASSERT_X statement will output the following message using the qFatal() function:

     ASSERT failure in divide: "division by zero", file div.cpp, line 7

See also Q_ASSERT(), qFatal(), and Debugging Techniques.

### void Q_CHECK_PTR(void *pointer)

If pointer is `nullptr`, prints a message containing the source code's file name and line number, saying that the program ran out of memory and aborts program execution. It throws `std::bad_alloc` instead if exceptions are enabled.

Q_CHECK_PTR does nothing if `QT_NO_DEBUG` and `QT_NO_EXCEPTIONS` were defined during compilation. Therefore you must not use Q_CHECK_PTR to check for successful memory allocations because the check will be disabled in some cases.

Example:

     int *a;

     Q_CHECK_PTR(a = new int[80]);   // WRONG!

     a = new (nothrow) int[80];      // Right
     Q_CHECK_PTR(a);

See also qWarning() and Debugging Techniques.

### void Q_UNREACHABLE()

Tells the compiler that the current point cannot be reached by any execution, so it may optimize any code paths leading here as dead code, as well as code continuing from here.

This macro is useful to mark impossible conditions. For example, given the following enum:

        enum Shapes {
            Rectangle,
            Triangle,
            Circle,
            NumShapes
        };

One can write a switch table like so:

        switch (shape) {
            case Rectangle:
                return rectangle();
            case Triangle:
                return triangle();
            case Circle:
                return circle();
            case NumShapes:
                Q_UNREACHABLE();
                break;
        }

The advantage of inserting Q_UNREACHABLE() at that point is that the compiler is told not to generate code for a shape variable containing that value. If the macro is missing, the compiler will still generate the necessary comparisons for that value. If the case label were removed, some compilers could produce a warning that some enum values were not checked.

By using this macro in impossible conditions, code coverage may be improved as dead code paths may be eliminated.

In debug builds the condition is enforced by an assert to facilitate debugging.

Note: Use the macro Q_UNREACHABLE_RETURN() to insert return statements for compilers that need them, without causing warnings for compilers that complain about its presence.

See also Q_ASSERT(), qFatal(), and Q_UNREACHABLE_RETURN().

### `[since 6.5]` void Q_UNREACHABLE_RETURN(...)

This is equivalent to

     Q_UNREACHABLE();
     return __VA_ARGS__;

except it omits the return on compilers that would warn about it.

This macro was introduced in Qt 6.5.

See also Q_UNREACHABLE().