From af051f9be230f06eba587c25d56837fb48fcc9ee Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 14 Feb 2024 09:52:22 +0100 Subject: [PATCH] QVarLengthArray: re-publish Prealloc as a nested PreallocatedSize This gives users of the class easy access to the Prealloc template argument, without having to write a pattern-matcher like template constexpr qsizetype preallocated_size_v; template constexpr qsizetype preallocated_size_v> = N; first. [ChangeLog][QtCore][QVarLengthArray] Added PreallocatedSize nested constant, equal to the Prealloc template argument. Change-Id: I928eaa5e62967445cdd7b0c2759567483fdb8997 Reviewed-by: Volker Hilsheimer --- src/corelib/tools/qvarlengtharray.h | 2 ++ src/corelib/tools/qvarlengtharray.qdoc | 9 +++++++++ .../tools/qvarlengtharray/tst_qvarlengtharray.cpp | 10 ++++++++++ 3 files changed, 21 insertions(+) diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index db04b683b1..8fa6da9a9a 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -278,6 +278,8 @@ class QVarLengthArray template using if_input_iterator = QtPrivate::IfIsInputIterator; public: + static constexpr qsizetype PreallocatedSize = Prealloc; + using size_type = typename Base::size_type; using value_type = typename Base::value_type; using pointer = typename Base::pointer; diff --git a/src/corelib/tools/qvarlengtharray.qdoc b/src/corelib/tools/qvarlengtharray.qdoc index e6d7cc16ec..4ebbcc43dc 100644 --- a/src/corelib/tools/qvarlengtharray.qdoc +++ b/src/corelib/tools/qvarlengtharray.qdoc @@ -462,6 +462,15 @@ \a defaultValue. */ +/* + \var QVarLengthArray::PreallocatedSize + \since 6.8 + + The same value as the \c{Prealloc} template argument. Provided for easier + access compared to manually extracting the value from the template + argument. +*/ + /*! \typedef QVarLengthArray::size_type \since 4.7 diff --git a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp index dcee8fd705..6a92663bc4 100644 --- a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp +++ b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp @@ -66,6 +66,7 @@ private slots: void sizeConstructor_QString() { sizeConstructor(); } void sizeConstructor_NonCopyable() { sizeConstructor(); } void append(); + void preallocatedSize(); #if QT_DEPRECATED_SINCE(6, 3) void prepend(); #endif @@ -194,6 +195,15 @@ void tst_QVarLengthArray::append() v2.append(5); } +void tst_QVarLengthArray::preallocatedSize() +{ + // The default is 256: + static_assert(QVarLengthArray::PreallocatedSize == 256); + // Otherwise, whatever was given as template argument: + static_assert(QVarLengthArray::PreallocatedSize == 42); + static_assert(QVarLengthArray::PreallocatedSize == 1'000'000); +} + #if QT_DEPRECATED_SINCE(6, 3) QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED