我只是在阅读Qt4源代码,发现预编译器在qstring.h .h(和其他位置)中多次定义Q_REQUIRED_RESULT
。
它实际上做了什么,为什么它没有任何文档(是否适合这里)?
它的定义如下:
#ifndef Q_REQUIRED_RESULT
# if defined(Q_CC_GNU) && !defined(Q_CC_INTEL) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1))
# define Q_REQUIRED_RESULT __attribute__ ((warn_unused_result))
# else
# define Q_REQUIRED_RESULT
# endif
#endif
发布于 2017-02-14 12:18:03
如果不使用函数的返回值,编译器就会生成警告,因为这很可能是在犯错误。例如:
QString str("hello, world!");
str.toUpper();
// str is still lower case, the upper case version has been
// *returned* from toUpper() and lost. the compiler should warn about this!
在C++17中,这已经在属性下标准化了。它没有文档化,因为它不是公共API --也就是说,在代码中冒险使用它,Qt可以随时更改它。(好,极不可能,但仍有可能)。
https://stackoverflow.com/questions/42225724
复制相似问题