On Tuesday, March 4th, 2025 at 9:44 AM, Arthur O'Dwyer via Std-Proposals <std-proposals@lists.isocpp.org> wrote:
C++ has a literal range type: std::initializer_list. However, its elements are const and so initializing a container from the elements involves a copy.
[...] A suitable proposal in this area should be able to compile
std::vector<std::unique_ptr<int>> v = { std::make_unique<int>(1), std::make_unique<int>(2) };
right out of the box. If we aren't enabling people to write that, then we aren't really helping them.
If someone is designing their own container,
a simple rvalue reference to array is usually
enough
template<size_t N>
vector(T (&&arr)[N]);
// use
vector v1({2, 4}); vector v2({std::make_unique<int>(1), std::make_unique<int>(2)});
std::vector<std::unique_ptr<int>> w(
std::from_range,
std::array{
std::make_unique<int>(1),
std::make_unique<int>(2),
} | std::views::as_rvalue
);
so, anything that requires this general level of ceremony around it isn't worth the bother of standardizing. Simply replacing `std::array{...} | rv::as_rvalue` with `std::ranges::literal{...}` doesn't rise to the occasion.
–Arthur
For standard containers, since the from_range_t tag
is already there, we could add an extra T(&&)[N] overload
to simplify this down to
std::vector w( std::from_range,
{
std::make_unique<int>(1),
std::make_unique<int>(2),
}
);
--
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
_______________________________________________