0
當編譯該樣本與MSVC(full source)MSVC:爲什麼包含基本QString操作的函數未內聯?
#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-bit MSVC
,VS 2013 Update 5, 64-bit 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()被內聯?
你看看asm來檢查是否確實調用了'makeString'。也許這個調用與構造函數和RVO一樣,但沒有標記爲*內聯*。 – Jarod42
只是一個懷疑,但'sharedNull'事情最終在'QArrayData'中使用了一個靜態字符數組 - 編譯器可能寧願不內聯並且有大量的地址要在該鏈接時修補,或者如果它加載時最終在一個共享對象.... –
@ Jarod42,是的,makeString是生成和調用。我已將此添加到我的帖子中。 – ens