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

问题描述 投票:1回答:1

我在constexpr函数中有一个循环,它在Xcode中编译但在Qt控制台中编译。

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

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

  • 设置CONFIG += c++17和/或QMAKE_CXXFLAGS += -std=c++17;
  • 用命名函数替换lambda;
  • do循环,while循环和for循环替换while-goto循环。

例如,在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
c++ qt qt-creator c++17
1个回答
2
投票

问题出在你的命令中:-std=gnu++11

你正在编译C ++ 11,而在C ++ 11中,constexpr函数仅限于一个语句 - 没有循环。

切换到-std=c++14-std=c++17,事情会变得更好。

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

© www.soinside.com 2019 - 2024. All rights reserved.