`qt_noop`的目的是什么?

gre*_*eth 6 c++ qt no-op

我刚刚qt_noop()在qglobal.h中发现了define 的存在:

inline void qt_noop() {}
Run Code Online (Sandbox Code Playgroud)

有什么意义呢?

cha*_*lup 8

我知道它在内部用于一些宏,它们应该只为调试版本做一些事情,例如:

#  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
Run Code Online (Sandbox Code Playgroud)

  • 在第二个定义中不能省略.Q_ASSERT宏通常像普通函数调用一样使用,即`Q_ASSERT(xxx == yyy);`.因此宏必须扩展为可以在分号后面跟随的代码,而不会出现任何错误/警告.对于"单个if",宏存在一些问题,如果它们在其他if语句中使用时没有括号(参见http://www.parashift.com/c++-faq-lite/misc-technical-issues. html#faq-39.4),所以这里的三元运算符可能是个更好的主意. (3认同)