如何在Qt Creator中的constexpr函数中使用循环?

时间:2019-04-18 23:44:37

标签: c++ qt qt-creator c++17

我在constexpr函数内部有一个循环,该函数在Xcode中进行编译,而在Qt Console中不进行编译。

我正在Qt Creator 4.9.0-Qt 5.12.2(Clang 10.0(Apple),64位)和带有编译器标记-std=c++17的Xcode 10.1中使用C ++ 17。

在Qt Console .pro文件中,我尝试过:

  • 设置CONFIG += c++17和/或QMAKE_CXXFLAGS += -std=c++17

  • 使用命名函数替换lambda;

  • 使用do循环,while循环和for循环替换while-goto循环。

    < / li>

例如,在Qt中,下面的程序出现错误:

  

“错误:constexpr函数中不允许使用语句”

带下划线的“ do”。

#include <iostream>
#include <array>
#include <cstdint>

constexpr auto least_significant_bit(uint64_t bits) {
    constexpr uint64_t magic = 0x07edd5e59a4e28c2ULL;
    constexpr auto lsb_map = []() constexpr {
        std::array<int, 64> result {0};
        uint64_t bit = 1; int i = 0;
        do { // problem
            result [bit * magic >> 58] = i;
            i++;
            bit <<= 1;
        } while(bit);
        return result;
    }();
    return lsb_map[(bits & -bits) * magic >> 58];
}

int main(int argc, const char * argv[]) {
    std::cout << least_significant_bit(0b10000100010000ULL) << std::endl;
}

我要怎么做才能使包含循环的constexpr函数在Qt中编译?预期输出为4。

以下是构建输出:

02:09:09: Running steps for project test...
02:09:09: Configuration unchanged, skipping qmake step.
02:09:09: Starting: "/usr/bin/make" -j8
/Users/freddiewoodruff/Qt/5.11.1/clang_64/bin/qmake -o Makefile ../test/test.pro -spec macx-clang CONFIG+=debug CONFIG+=x86_64 CONFIG+=qml_debug
/Library/Developer/CommandLineTools/usr/bin/clang++ -c -pipe -stdlib=libc++ -std=c++17 -g -std=gnu++11  -arch x86_64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk -mmacosx-version-min=10.11 -Wall -W -fPIC -DQT_QML_DEBUG -I../test -I. -I/Users/freddiewoodruff/Qt/5.11.1/clang_64/mkspecs/macx-clang -o main.o ../test/main.cpp
../test/main.cpp:5:11: error: 'auto' return without trailing return type; deduced return types are a C++14 extension
constexpr auto least_significant_bit(uint64_t bits) {
          ^
../test/main.cpp:7:35: warning: 'constexpr' on lambda expressions is a C++17 extension [-Wc++17-extensions]
    constexpr auto lsb_map = []() constexpr {
                                  ^
../test/main.cpp:8:29: warning: variable declaration in a constexpr function is a C++14 extension [-Wc++14-extensions]
        std::array<int, 64> result {0};
                            ^
../test/main.cpp:9:18: warning: variable declaration in a constexpr function is a C++14 extension [-Wc++14-extensions]
        uint64_t bit = 1; int i = 0;
                 ^
../test/main.cpp:9:31: warning: variable declaration in a constexpr function is a C++14 extension [-Wc++14-extensions]
        uint64_t bit = 1; int i = 0;
                              ^
../test/main.cpp:10:9: error: statement not allowed in constexpr function
        do { // problem
        ^
../test/main.cpp:20:14: warning: unused parameter 'argc' [-Wunused-parameter]
int main(int argc, const char * argv[]) {
             ^
../test/main.cpp:20:33: warning: unused parameter 'argv' [-Wunused-parameter]
int main(int argc, const char * argv[]) {
                                ^
6 warnings and 2 errors generated.
make: *** [main.o] Error 1
02:09:10: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project test (kit: Desktop Qt 5.11.1 clang 64bit)
When executing step "Make"
02:09:10: Elapsed time: 00:01.

这是.pro文件:

TEMPLATE = app
CONFIG += -std=c++17
QMAKE_CXXFLAGS += -std=c++17
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += \
        main.cpp

1 个答案:

答案 0 :(得分:2)

问题出在您的命令-std=gnu++11中。

您正在为C ++ 11进行编译,在C ++ 11中,constexpr函数被限制为单个语句-没有循环。

将该位切换为-std=c++14-std=c++17,情况会变得更好。

[稍后:即使您说您正在使用C ++ 17,构建日志也显示-std=gnu++11]