使用 MSVC ( full source ) 编译此示例时
#include <QString>
#include <QDebug>
static __forceinline QString makeString() {
return {};
}
int main()
{
QString str = makeString();
qDebug() << str;
return 0;
}
发出以下警告:
function 'QString makeString(void)' marked as __forceinline not inlined
对于编译,我使用默认的 qmake 设置(加上 -W4
警告):
cl -c -nologo -Zc:wchar_t -FS -Zc:strictStrings -Zc:throwingNew -O2 -MD -GR -W4 -EHsc
-w34189 -w44996 -w44456 -w44457 -w44458 -wd4577 -wd4127 -w34100
-DUNICODE -DWIN32 -DWIN64 -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DNDEBUG
-IC:\home\dev\qstring_inline -I. -I..\..\qt\qt_5.5.1\build\qtbase\include -I..\..\qt\qt_5.5.1\build\qtbase\include\QtGui -I..\..\qt\qt_5.5.1\build\qtbase\include\QtCore -Irelease -Ie:\builds\qt\qt_5.5.1\qt5\qtbase\mkspecs\win32-msvc2015 -Forelease\ @C:\Users\ens\AppData\Local\Temp\main.obj.17344.16.jom
main.cc
我用 Qt 5.5.1
、Qt 5.4
和 VS 2015 Update 1,64 位 MSVC
重现了这个问题,VS 2013 更新 5,64 位 MSVC
。
检查程序集确认确实生成并调用了 makeString()。
默认构造函数(code browser)...
inline QString::QString() Q_DECL_NOTHROW : d(Data::sharedNull()) {}
...和右值复制构造函数(code browser)
inline QString(QString && other) Q_DECL_NOTHROW : d(other.d) { other.d = Data::sharedNull(); }
非常简单。 什么可以阻止 makeString() 被内联?
最佳答案
编译器无法内联具有非平凡析构函数的返回类型的函数。这是编译器的限制;编译器团队正在考虑在未来的版本中对此进行改进。
关于c++ - MSVC : Why are functions containing basic QString operations not inlined?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34331617/