У меня есть цикл внутри функции constexpr
, которая компилируется в Xcode, но не в Qt Console.
Я использую C ++ 17 в Qt Creator 4.9.0 - Qt 5.12.2 (Clang10.0 (Apple), 64-разрядная) и Xcode 10.1 с флагом компилятора -std=c++17
.
В файле .pro консоли Qt я попытался:
параметрCONFIG += c++17
и / или QMAKE_CXXFLAGS += -std=c++17
;
замена лямбды на именованную функцию;
замена do
- while
loop с циклом 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