1 From 3312e89b47f8c2ea0b4263b39841c25b83a37332 Mon Sep 17 00:00:00 2001
2 From: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
3 Date: Fri, 16 Jan 2026 21:54:45 +0100
4 Subject: [PATCH] QStringView: fix benign ODR violation for
5 count(QRegularExpression)
6 7 QRegularExpression is only forward declared in qstringview.h.
8 QStringView::count(const QRegularExpression &re) simply calls
9 `QtPrivate::count(*this, re)`. The problem is that this latter
10 count is overloaded, and there's a `QtPrivate::count(QStringView,
11 QStringView)` overload available.
12 13 This overload is not viable because there is no conversion from
14 QRegularExpression to QStringView. To determine this, the compiler
15 instantiates the QStringView(const Container &) constructor template,
16 with Container = QRegularExpression. This function template is
17 constrained via SFINAE, and it will fail the constraint checks
18 _because QRegularExpression is incomplete_ (in particular std::data
19 itself has SFINAE, and it fails in there).
20 21 GCC doesn't like the idea that at a later time we complete
22 QRegularExpression, because it fears that the prior result might
23 have been different had QRegularExpression been complete.
24 25 We know it's not different, but still, silence the warning by
26 moving the call to QtPrivate::count where QRegularExpression
27 is complete.
28 29 Pick-to: 6.11
30 Change-Id: I294c5ccb7c4ab3d52e518182c159e690575cbb00
31 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
32 ---
33 src/corelib/text/qregularexpression.h | 5 +++++
34 src/corelib/text/qstringview.h | 5 +----
35 2 files changed, 6 insertions(+), 4 deletions(-)
36 37 diff --git a/qtbase/src/corelib/text/qregularexpression.h b/qtbase/src/corelib/text/qregularexpression.h
38 index 462786179cb5..ece094ca768f 100644
39 --- a/qtbase/src/corelib/text/qregularexpression.h
40 +++ b/qtbase/src/corelib/text/qregularexpression.h
41 @@ -191,6 +191,11 @@ Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QRegularExpression &re);
42 Q_CORE_EXPORT QDebug operator<<(QDebug debug, QRegularExpression::PatternOptions patternOptions);
43 #endif
44 45 +[[nodiscard]] inline qsizetype QStringView::count(const QRegularExpression &re) const
46 +{
47 + return QtPrivate::count(*this, re);
48 +}
49 +
50 struct QRegularExpressionMatchPrivate;
51 QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(QRegularExpressionMatchPrivate, Q_CORE_EXPORT)
52 53 diff --git a/qtbase/src/corelib/text/qstringview.h b/qtbase/src/corelib/text/qstringview.h
54 index d586620c8b09..6d5edfc06d7d 100644
55 --- a/qtbase/src/corelib/text/qstringview.h
56 +++ b/qtbase/src/corelib/text/qstringview.h
57 @@ -329,10 +329,7 @@ class QStringView
58 {
59 return QtPrivate::contains(*this, re, rmatch);
60 }
61 - [[nodiscard]] qsizetype count(const QRegularExpression &re) const
62 - {
63 - return QtPrivate::count(*this, re);
64 - }
65 + [[nodiscard]] qsizetype count(const QRegularExpression &re) const; // defined in qregularexpression.h
66 #endif
67 68 [[nodiscard]] bool isRightToLeft() const noexcept
69