<QtAssert> Proxy Page

函数

T * q_check_ptr (T * p )

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
void Q_UNREACHABLE_RETURN (...)

函数文档编制

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 .

宏文档编制

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.

范例:

// File: div.cpp
#include <QtGlobal>
int divide(int a, int b)
{
    Q_ASSERT(b != 0);
    return a / b;
}
					

b is zero, the Q_ASSERT statement will output the following message using the qFatal () 函数:

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

另请参阅 Q_ASSERT_X (), qFatal (),和 调试技术 .

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.

范例:

// File: div.cpp
#include <QtGlobal>
int divide(int a, int b)
{
    Q_ASSERT_X(b != 0, "divide", "division by zero");
    return a / b;
}
					

b is zero, the Q_ASSERT_X statement will output the following message using the qFatal () 函数:

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

另请参阅 Q_ASSERT (), qFatal (),和 调试技术 .

void Q_CHECK_PTR ( void * pointer )

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.

范例:

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

另请参阅 qWarning () 和 调试技术 .

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.

注意: 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.

另请参阅 Q_ASSERT (), qFatal (),和 Q_UNREACHABLE_RETURN ().

[since 6.5] void Q_UNREACHABLE_RETURN (...)

这相当于

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.

另请参阅 Q_UNREACHABLE ().