Skip to content

Add count-value constructors to VariableLengthArray (#63) - #241

Merged
thirtytwobits merged 2 commits into
mainfrom
claude/issue-63-vla-count-value-ctor
Oct 19, 2026
Merged

thirtytwobits merged 2 commits into
mainfrom
claude/issue-63-vla-count-value-ctor

Conversation

@thirtytwobits

Copy link
Copy Markdown
Member

Adds the count-value constructors to cetl::VariableLengthArray, matching std::vector::vector (3):

VariableLengthArray(count, value, alloc);
VariableLengthArray(count, value, max_size_max, alloc);

Both the general and the packed-bool versions:

  • construct count copies of value, using exactly the storage needed (whole bytes for bool)
  • throw std::length_error when count is greater than max_size(), and std::bad_alloc when allocation fails
  • with exceptions disabled, truncate to max_size() or to the storage the allocator provides, which you can detect by comparing size() with count

Conflicts with the existing constructors (the issue asked for this check)

  • Initializer list + max_size_max. With a plain size_type count parameter, VLA<int>({x}, 5, alloc) becomes ambiguous. The braced list prefers initializer_list, but the int literal 5 converts better to const int& than to size_type. I confirmed the ambiguity with both GCC and Clang. To avoid it, the count's type is deduced, accepting any type convertible to size_type. A braced argument can't be deduced, so that call still picks the initializer-list constructor.
  • (n, alloc). This still sets max_size_max. There is no std::vector-style (count, alloc) overload, because it would collide with that constructor. The docs say so.
  • Range constructors. These are unchanged. Iterators, including pointers for VLA<std::size_t> and VLA<int*>, still select them.
  • Integer arguments. VLA<int>(3, 4, alloc) used to be ill-formed. It now constructs three 4s, as std::vector does. The assign suite's "non-iterators must not select the range constructor" assertions relied on the old behaviour, so they now use nullptr_t arguments, which are neither iterators nor counts.

Tests

  • New test_variable_length_array_construct.cpp, typed over int and bool and checked against std::vector. It covers byte boundaries, max_size_max, counts larger than max_size(), different count types (including an unscoped enum), and overload resolution against the max-size, initializer-list (parenthesised and braced) and range constructors, with size_type, pointer and std::string elements.
  • test_variable_length_array_exception_safety.cpp adds count-value cases for throwing element copies and allocator construction-hook failures, plus a new test for length_error and bad_alloc that checks nothing is constructed or allocated.
  • test_variable_length_array_compat.cpp adds a std::string count-value case that runs against both VLA and std::vector.
  • The fuzzer's CopyConstruct operation now also checks count-value construction against the model. The input format is unchanged.

Validation

  • GCC 13, C++14: all 12 VLA suites pass in Debug and DebugEP (-fno-exceptions)
  • GCC 7.5 (toolshed ts24.4.3), C++14: the construct, exception-safety, compat and assign suites pass with and without exceptions
  • GCC 13 and Clang, C++14/17/20, with and without exceptions: those four suites compile cleanly with -Wall -Wextra -Werror -Wconversion -Wsign-conversion -pedantic
  • Clang, C++20, ASan + UBSan: those four suites pass
  • libFuzzer, default and embedded profiles: ctest passes, plus 60 s runs of about 650k inputs each with no findings
  • clang-format 19.1.7 from toolshed:ts24.4.3 reports no changes for any line I touched; git diff --check is clean. Some untouched lines in the compat and exception-safety tests already differ from the format on main, and I left them alone.

Closes #63.

🤖 Generated with Claude Code

Add `VariableLengthArray(count, value, alloc)`, matching std::vector's
constructor (3), and `VariableLengthArray(count, value, max_size_max,
alloc)`, for both ordinary elements and packed bool. They construct
`count` copies of `value` in exactly the storage needed, throw
std::length_error when `count` exceeds max_size(), and, with exceptions
disabled, truncate to max_size() or to the storage the allocator
provides.

The count's type is deduced (any type convertible to size_type) instead
of being a plain size_type. A plain size_type parameter makes the
existing `VLA<int>({x}, 5, alloc)` ambiguous with the initializer-list
constructor: the braced list prefers initializer_list, but the int
literal 5 converts better to `const int&` than to size_type. A braced
argument cannot be deduced, so that call still selects the
initializer-list constructor. There is no `(count, alloc)` overload,
because `(n, alloc)` already sets max_size_max.

Integer arguments such as `VLA<int>(3, 4, alloc)` used to be ill-formed
and now construct three 4s, as std::vector does; the assign suite's
"non-iterators must not select the range constructor" assertions now
use nullptr_t arguments, which are neither iterators nor counts.

Tests: a new test_variable_length_array_construct suite (int and bool,
checked against std::vector) covers byte boundaries, max_size_max,
oversized counts, count types, and overload resolution against the
max_size, initializer-list, and range constructors, including
size_type and pointer elements. The exception-safety suite covers
throwing copies, allocator construction-hook failures, length_error,
and bad_alloc; the compat suite runs a std::string case against
std::vector; and the fuzzer checks count-value construction with
CopyConstruct.

Closes #63.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thirtytwobits

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T00:12:49.897530Z db639ba Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: db639ba303

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread include/cetl/variable_length_array.hpp Outdated
When value_type is itself an input iterator that converts to size_type,
`VLA(first, last, alloc)` and `VLA(first, last, max_size_max, alloc)`
matched the count-value and range constructor templates equally well and
were ambiguous, although they compiled before the count-value
constructors existed. std::vector selects its range constructor here.

Add a SFINAE-friendly detail::is_input_iterator trait, use it for the
existing range-overload constraint, and exclude input iterators from
enable_if_count_t, which both count-value overloads in both
specializations share. A new test checks range selection against
std::vector for such an element type and for packed bool.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thirtytwobits
thirtytwobits merged commit dfe0606 into main Oct 19, 2026
29 checks passed
@thirtytwobits
thirtytwobits deleted the claude/issue-63-vla-count-value-ctor branch October 19, 2026 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add T-value constructors to VLA

1 participant