C++ Logo

std-proposals

Advanced search

std::to_array support for zero-length arrays

From: D'Alessandro, Luke K <ldalessa_at_[hidden]>
Date: Wed, 4 Nov 2020 04:01:40 +0000
Does this seem like something I should go ahead and put together officially?

Consider that it is currently impossible, per [array.creation], to use std::to_array to create a zero-length array. We believe that this is merely an oversight, and not intentional.

We propose to extend [array.creation] with the following `to_array` overload.

template <typename T>
constexpr array<T, 0> to_array(std::array<T, 0>);

Mandates: is_array_v<T> is false.
Preconditions: none
Returns: {}

This provides a valid match for std::to_array<T>({}) and should not be ambiguous with either of the existing overloads.

I see no particular constraints on T in [array.zero], nor do any of the constructor traits appear to be relevant here, thus I have no preconditions and merely carried forward the is_array_v mandate from the existing overloads.

Thanks to Will Ray and Darrell Wright for collaboration on the proposed fix.

See https://godbolt.org/z/r31Yn1 for a working example.

Luke

----
Luke D’Alessandro, Ph.D.
Department of Intelligent Systems Engineering
Indiana University Bloomington
ldalessa_at_[hidden]<mailto:ldalessa_at_iu.edu>


Received on 2020-11-03 22:01:43