Skip to content

Enhancement: Allow ystdlib::containers::Array to support initializer list construction with non-default-constructible types #35

@coderabbitai

Description

@coderabbitai

Background

Currently, the Array class in src/ystdlib/containers/Array.hpp has a limitation: it does not support initialization with an initializer list when the contained type lacks a default constructor. This restriction contrasts with std::array, which allows such initialization.

Consider the following class A, which has no default constructor:

class A {
public:
    A() = delete;
    A(size_t value) : m_value{value} {}
    [[nodiscard]] auto get_value() const -> size_t { return m_value; }
private:
    size_t m_value;
};

std::array<A, 3> obj{1, 2, 3};  // ✅ Compiles successfully
ystdlib::containers::Array<A> obj{1, 2, 3};  // ❌ Compile-time error

This limitation stems from the underlying data structure used in Array, which is std::unique_ptr<T[]>. Since the array memory must be allocated before values can be assigned, the type T must be default-constructible.

To address this, modifications to Array's internal storage mechanism may be required to allow direct initialization of non-default-constructible types from an initializer list.

Requirements

  • Update Array implementation to support list initialization with non-default-initializable types.
  • Add appropriate unit tests to verify the behavior.
  • Update documentation (remove TODOs to reflect the enhanced capabilities).

Related PR

This was identified in PR #33

Requested by

@Bill-hbrhbr

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions