Я пытаюсь получить идентификатор QMetaType
во время компиляции, однако, когда я пробую этот очень простой базовый случай:
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(helloqt)
set(CMAKE_CXX_STANDARD 14)
find_package(Qt5Widgets REQUIRED)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(helloqt Qt5::Widgets)
main.cpp
#include <QApplication>
#include <QDebug>
struct Test{
};
Q_DECLARE_METATYPE (Test)
constexpr int test_enum = qMetaTypeId<Test>();
int main() {
qDebug() << test_enum;
return 0;
}
Я получаю следующую ошибку:
In file included from C:/msys64/mingw64/include/QtCore/qobject.h:54:0,
from C:/msys64/mingw64/include/QtCore/qcoreapplication.h:46,
from C:/msys64/mingw64/include/QtWidgets/qapplication.h:44,
from C:/msys64/mingw64/include/QtWidgets/QApplication:1,
from ...\main.cpp:1:
...\main.cpp:8:51: in constexpr expansion of 'qMetaTypeId<Test>()'
C:/msys64/mingw64/include/QtCore/qmetatype.h:1754:43: error: 'static constexpr int QMetaTypeId2<T>::qt_metatype_id() [with T = Test]' called in a constant expression
return QMetaTypeId2<T>::qt_metatype_id();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
C:/msys64/mingw64/include/QtCore/qmetatype.h:1618:40: note: 'static constexpr int QMetaTypeId2<T>::qt_metatype_id() [with T = Test]' is not usable as a constexpr function because:
static inline Q_DECL_CONSTEXPR int qt_metatype_id() { return QMetaTypeId<T>::qt_metatype_id(); }
^~~~~~~~~~~~~~
C:/msys64/mingw64/include/QtCore/qmetatype.h:1618:96: error: call to non-constexpr function 'static int QMetaTypeId<Test>::qt_metatype_id()'
static inline Q_DECL_CONSTEXPR int qt_metatype_id() { return QMetaTypeId<T>::qt_metatype_id(); }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
mingw32-make.exe[3]: *** [CMakeFiles\helloqt.dir\build.make:63: CMakeFiles/helloqt.dir/main.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:67: CMakeFiles/helloqt.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:79: CMakeFiles/helloqt.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: helloqt] Error 2
Похоже, что qMetaTypeID не является константным выражением, но это то, что я вижу в моем редакторе.
, и этот код прекрасно компилируется:
#include <QApplication>
#include <QDebug>
Q_DECL_CONSTEXPR int x = 3;
int main() {
static_assert(x == 3);
return 0;
}
(я протестировал без использования constexpr, и он не работает, чтокак и ожидалось).
Мой компилятор - Mingw-64 с версией QT 5.10.1.