[Development] static constexpr in exported classes needs out-of-line definitions
Thiago Macieira
thiago.macieira at intel.com
Thu Jan 18 04:35:17 CET 2024
See http://bugreports.qt.io/browse/QTBUG-121135 for the issue and
https://codereview.qt-project.org/c/qt/qtbase/+/531251 for the solution.
TL;DR: if you define a static constexpr (which is implicitly inline) member
variable in a class with export macro, you MUST add the definition of tha
variable to the .cpp.
Alternatives:
* move to a class that is not exported
trick: add an empty base with the variable
* change to enum (if primitive)
But this happens even for plain int variables. For example:
#include <vector>
#include <QtCore/qlocale.h>
void f(std::vector<int> &v)
{
v.push_back(QLocale::FirstTwoDigitYear);
}
This produced: with GCC 13 at -O2 optimisations:
_Z1fRSt6vectorIiSaIiEE:
movq 8(%rcx), %rdx
cmpq 16(%rcx), %rdx
je .L29
movl $1900, (%rdx)
addq $4, %rdx
movq %rdx, 8(%rcx)
ret
.L29:
movq __imp__ZN7QLocale17FirstTwoDigitYearE(%rip), %r8
jmp
_ZNSt6vectorIiSaIiEE17_M_realloc_insertIJRKiEEEvN9__gnu_cxx17__normal_iteratorIPiS1_EEDpOT_
Interpretation: if the vector has enough pre-allocated space, it simply moves
1900 to the end of te array and adjusts the end. But if not, it calls
std::vector::::_M_realloc_insert(), which takes the integer by reference, so
the compiler emits an import from the DLL.
This appears to be a GCC bug/shortcoming. Clang and MSVC apparently
automatically emit and export the variable for you:
https://mingw.godbolt.org/z/q4dYdosjh
--
Thiago Macieira - thiago.macieira (AT) intel.com
Cloud Software Architect - Intel DCAI Cloud Engineering
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 5163 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/development/attachments/20240117/22701805/attachment.bin>
More information about the Development
mailing list