Q_ASSERT 发布构建语义 [英] Q_ASSERT release build semantics
问题描述
我找不到关于发布版本下 Q_ASSERT 语义的明确声明.如果没有断言检查,那么是否对断言表达式进行了评估?
I can't find a clear statement on the semantics of Q_ASSERT under release builds. If there is no assertion checking, then is the asserted expression evaluated at all?
考虑下面的代码
Q_ASSERT(do_something_report_false_if_failed());
do_something_report_false_if_failed()
会在所有潜在的 Qt 构建配置下运行吗?改为这样做是否更安全(即使更冗长且可读性更差):
Will do_something_report_false_if_failed()
run under all potential Qt build configurations? Would it be safer (even though a bit more verbose and less readable) to do this instead:
bool is_ok = do_something_report_false_if_failed();
Q_ASSERT(is_ok)
后一种方法的缺点是 ASSERT 失败不那么冗长,但也许它更清楚地表明语句已执行?
The latter approach has the downside that ASSERT failures are less verbose, but perhaps it shows more clearly that the statement is executed?
推荐答案
Q_ASSERT
中的表达式将不在非调试构建配置中进行评估.
The expression inside the Q_ASSERT
will not be evaluated in non-debug build configurations.
考虑以下来自 Qt 存储库.
#if !defined(Q_ASSERT)
# ifndef QT_NO_DEBUG
# define Q_ASSERT(cond) ((!(cond)) ? qt_assert(#cond,__FILE__,__LINE__) : qt_noop())
# else
# define Q_ASSERT(cond) qt_noop()
# endif
#endif
如果定义了 QT_NO_DEBUG
,则整个 Q_ASSERT
语句将替换为 qt_noop()
,从而删除它之前包含的任何表达式.
If QT_NO_DEBUG
is defined, then the entire Q_ASSERT
statement is replaced with a qt_noop()
, thereby removing any expression that it previously contained.
永远不要依赖 Q_ASSERT
语句中的表达式产生的任何副作用.从技术上讲,仍然可以确保 QT_NO_DEBUG
未在特定构建配置中定义,但这不是一个好主意™.
Never rely on any side effects created by an expression inside a Q_ASSERT
statement. Technically it is still possible to ensure that QT_NO_DEBUG
is not defined in a specific build configuration, but this is not a Good Idea™.
这篇关于Q_ASSERT 发布构建语义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!