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 <typename T>
   constexpr qsizetype preallocated_size_v;
   template <typename T, qsizetype N>
   constexpr qsizetype preallocated_size_v<QVarLengthArray<T,N>> = N;

first.

[ChangeLog][QtCore][QVarLengthArray] Added PreallocatedSize nested
constant, equal to the Prealloc template argument.

Change-Id: I928eaa5e62967445cdd7b0c2759567483fdb8997
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
bb10
Marc Mutz 2024-02-14 09:52:22 +01:00
parent e703feea40
commit af051f9be2
3 changed files with 21 additions and 0 deletions

View File

@ -278,6 +278,8 @@ class QVarLengthArray
template <typename InputIterator>
using if_input_iterator = QtPrivate::IfIsInputIterator<InputIterator>;
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;

View File

@ -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

View File

@ -66,6 +66,7 @@ private slots:
void sizeConstructor_QString() { sizeConstructor<QString>(); }
void sizeConstructor_NonCopyable() { sizeConstructor<NonCopyable>(); }
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<int>::PreallocatedSize == 256);
// Otherwise, whatever was given as template argument:
static_assert(QVarLengthArray<int, 42>::PreallocatedSize == 42);
static_assert(QVarLengthArray<int, 1'000'000>::PreallocatedSize == 1'000'000);
}
#if QT_DEPRECATED_SINCE(6, 3)
QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED