[Development] static constexpr in exported classes needs out-of-line definitions
Marc Mutz
marc.mutz at qt.io
Wed Jan 31 16:54:57 CET 2024
Thanks! Added to
https://wiki.qt.io/Things_To_Look_Out_For_In_Reviews#Variables
On 18.01.24 04:35, Thiago Macieira wrote:
> 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
>
>
--
Marc Mutz <marc.mutz at qt.io>
Principal Software Engineer
The Qt Company
Erich-Thilo-Str. 10 12489
Berlin, Germany
www.qt.io
Geschäftsführer: Mika Pälsi, Juha Varelius, Jouni Lintunen
Sitz der Gesellschaft: Berlin,
Registergericht: Amtsgericht Charlottenburg,
HRB 144331 B
More information about the Development
mailing list