Q_ASSERT发布构建语义 [英] Q_ASSERT release build semantics

查看:158
本文介绍了Q_ASSERT发布构建语义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发行版本中,我找不到关于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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆