[libcxx-commits] [libcxx] [libc++] Fix constexpr initialization of std::array<T, 0> (PR #74667)
Ryan Nicholl via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Dec 14 15:07:45 PST 2023
rnicholl-google wrote:
> > Hmm, So I see we already migrated away from not returning `nullptr` from `array<T, 0>`. My understanding was that was explicitly prohibited by the standard when it says
> > [2](https://eel.is/c++draft/sequences#array.zero-2) [#](https://github.com/Eelis/draft/tree/180690ab2654988b5d40bfe2f50b32737f02f20b/source/containers.tex#L6391) In the case that N == 0, begin() == end() == unique value[.](https://eel.is/c++draft/sequences#array.zero-2.sentence-1) The return value of data() is unspecified[.](https://eel.is/c++draft/sequences#array.zero-2.sentence-2)
> > The "unique" bit specifically saying that the two arrays can't both return nullptr.
> > I think we may have strayed from the right implementation here, and done it a while ago. I would like to consider this more.
>
> Just to be clear though, this patch doesn't affect whether we return `nullptr` or not from `begin()`, nor our ability to do so. It only fixes cases where we should be able to initialize an empty array inside `constexpr` and we currently can't, without breaking the ABI. If you wanted to return `nullptr` from `begin()` (which I think you can't without contradicting the standard), you could in theory do it orthogonally to this patch.
>
> Edit: It looks like we raced on this! Edit2: My comment is actually completely wrong. We are returning `nullptr` from `begin()` since [7265ff9](https://github.com/llvm/llvm-project/commit/7265ff928a974a844b6301c139cbb0f957532da9). And yes I agree with you that this is technically not standards-conforming, however that commit explains why all other implementations are also not standards-conforming.
I may have found a possible way to do a standard compliant implementation, so don't merge this yet, I'll try to have a patch within the week. I'm not sure if it'll work fully yet and pass all tests but I found a hack with unions that might make the non-nullptr version implementable.
https://github.com/llvm/llvm-project/pull/74667
More information about the libcxx-commits
mailing list