gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:17:08 27 4
gpt4 key购买 nike

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

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

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

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

  • 用命名函数替换 lambda;

  • do-while 循环替换为 for 循环、while 循环和goto 循环。

例如,在 Qt 中,对于下面的程序我得到一个错误:

"error: statement not allowed in constexpr function"

带有下划线的“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;
}

要在 Qt 中编译包含循环的 constexpr 函数,我需要做什么?预期输出为 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

最佳答案

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

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

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

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

关于c++ - 如何在 Qt Creator 的 constexpr 函数中使用循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55754970/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com