c++ - MSVC : Why are functions containing basic QString operations not inlined?

标签 c++ qt visual-c++

使用 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.1Qt 5.4VS 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/

相关文章:

c++ - 比较两个 vector 以获得公共(public)元素/定义 user1 和 user2

c++ - 我可以将 std::atomic<int64> 放置在共享内存中并期望原子操作吗?

c++ - 确保在外部进程中创建文件时刷新文件 (Win32)

C++ 什么时候值得前向声明?

c++ - 如何基于qmake创建可重用的库?

python - 如果定义了 itemchange(),则无法将项目添加到项目组 (PyQt)

c++ - 类(Class)关系的设计

c++ - 给定 IR 文件时,如何使 LLVM opt 输出 IR 文件?

c++ - 复制构造函数替换移动构造函数?

c++ - C++:将lambda作为带有模板参数的函数进行传递会使推导模板类型失败