Boost logo

Boost :

From: Antony Polukhin (antoshkka_at_[hidden])
Date: 2024-06-10 22:46:10


вт, 21 мая 2024 г. в 08:03, Ion Gaztañaga via Boost <boost_at_[hidden]>:
<...>
> Note that this document does not outline a schedule for all those ideas,
> it's an initial description of possible developments that should be
> evaluated/prioritized, as developing a substantial subset of the
> proposals would be a huge task.
>
> Thanks in advance for your attention and comments.

There's one container that I find really useful and that is not in a
list. It's a dynamically allocated array that knows its size and
allows initialization of elements with the initial value or via a
lambda.

If fills the gap between vector and unique_ptr<T[]>:
* works with non-movable and non-copyable types (unlike vector)
* works with not default constructible types (unlike unique_ptr<T[]>)
* has continuous layout
* usable with range based for (unlike unique_ptr<T[]>)
* smaller than vector (does not need to store capacity)

Here's a prototype
https://github.com/userver-framework/userver/blob/develop/universal/include/userver/utils/fixed_array.hpp
We called that container FixedArray, because it is an array of a fixed
size. And because it "fixes" the issues of dynamically allocated
arrays.

Usage examples:

  auto clients = utils::GenerateFixedArray(clients_count, [](int index) {
      return NonMovableClient(connection_string_to_client_with_index(index));
  });
  ASSERT_EQ(clients.size(), clients_count);
  // ...
  for (auto& client: clients) {
    client.SendRequest(x);
  }

Another example:
  FixedArray<std::atomic<int>> atomics(count, initial_value_for_each_atomic);

-- 
Best regards,
Antony Polukhin

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk