Can't use c++17 features using g++ 7.2 in QtCreator

后端 未结 3 1919
既然无缘
既然无缘 2020-12-16 10:36

I have recently updated gcc and g++ to version 7.2. I would like to try out std::experimental::any and std::variant in particular, and I am using Q

相关标签:
3条回答
  • CONFIG += c++17 can be used with Qt 5.12 and later.

    For Qt 5.11 and earlier, it is not a recognized QMake flag and you have to get your hands a bit dirty.

    Adding QMAKE_CXXFLAGS += -std=c++17 does the job for GCC & Clang; for MSVC you will probably need to specify /std:c++17 or /std:c++latest.

    0 讨论(0)
  • 2020-12-16 11:25

    For Windows: I'm download qt-opensource-windows-x86-5.11.1, icncluded MinGW32. For MinGW64 I'm download qt-5.5.0-x64-mingw510r0-seh-rev0 and install only compiler. Configure QtCreator, as says here. Create new project and add QMAKE_CXXFLAGS += -std=gnu++1z to .pro file (gcc doc). For test, try compile this simple code:

    #include <optional>
    
    std::optional<int> foo() 
    {
        return std::nullopt;
    }
    
    int main(int argc, char *argv[])
    {
        foo();
    }
    
    0 讨论(0)
  • 2020-12-16 11:26

    Edit 3/2019: You can use CONFIG += c++17 since Qt 5.12.


    The actual flag is c++1z, not c++17. In short, to get C++17 support, you don't need to modify QMAKE_CXXFLAGS and can instead simply use CONFIG += c++1z.

    Discussion on the reason why can be found in this bug report, but it pretty much amounts to "we implemented it as c++1z before C++17 was standardized, and now we won't bother aliasing it."

    Note: I realize you just needed a clean and rebuild. I'm answering the underlying question of "what flags do I need to use to enable C++17 support?"

    0 讨论(0)
提交回复
热议问题