Date: Thu, 17 Aug 2023 13:50:31 +0300
std::array provides some guarantees: that the N elements of type T are contiguous, that std::array is an aggregate (with further guarantees on how you can initialise it), that it is a structural type, etc.
AFAIK it does however does *not* provide a guarantee that there is an actual array, that is, a T[N], under the hood anywhere. A hostile implementation could implement std::array<T, 2> for example as a struct { T first, second; }; From this follows that std::array is *not* in general (at least as far as the Standard is concerned) pointer-interconvertible with a plain array or any other unrelated type.
In order to make this work, we need a new language facility along the lines of P1912 <https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1912r1.pdf>, which is a paper that I unfortunately didn't have time to work on further, but I am hoping to find some time in the future to pick it up again.
Cheers,
Timur
> On 17 Aug 2023, at 13:30, Lénárd Szolnoki via Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> On Thu, 2023-08-17 at 09:41 +0100, Gašper Ažman wrote:
>> Yes, what Lénárd said, unless you actually need to treat it as a
>> std::array, in which case I'm still interested in the answer to my
>> question.
>>
>
> Pointer interconvertibility still only allows to convert and access
> through the pointer if the object of the corresponding type is there.
> For some T std::array<T, N> should be implicit-lifetime, so you might
> be able to do create the object with start_lifetime_as (or self-
> memmove, if applicable). I'm not sure if accessing the elements through
> a reference or name to the original C array is defined after this
> operation though.
>
>>
>> On Thu, Aug 17, 2023, 09:27 Lénárd Szolnoki via Std-Proposals
>> <std-proposals_at_[hidden]> wrote:
>>> On Thu, 2023-08-17 at 09:12 +0100, Frederick Virchanza Gotham via
>>> Std-
>>> Proposals wrote:
>>>> On Wed, Aug 16, 2023 at 11:08 PM Jason McKesson wrote:
>>>>>>
>>>>>> std::array arr = {
>>>>>> something1,
>>>>>> something2,
>>>>>> something3,
>>>>>> };
>>>>>>
>>>>>> also works.
>>>>>
>>>>> That deduction guide has been there since C++17, so it's
>>>>> standard.
>>>>> Assuming of course that all of those are of the same type.
>>>>
>>>>
>>>> I'm looking through cppreference.com here and I don't see a
>>>> function
>>>> something like:
>>>>
>>>> template< typename T, std::size_t len >
>>>> std::array<T,len> &pretend_is_std_array( T (&arg)[len] ); //
>>>> returns
>>>> a reference
>>>>
>>>> What are we supposed to do if we want to treat a C-style array as
>>>> though it's an std::array? Should we just reinterpret_cast?
>>>
>>> No, use std::span. C arrays, std::array and even std::vector and
>>> subranges of these are compatible with std::span.
>>>
>>> Or if you are writing a template then use contiguous_range, if you
>>> have
>>> to make use of contiguity.
>>>
>>> Cheers,
>>> Lénárd
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
AFAIK it does however does *not* provide a guarantee that there is an actual array, that is, a T[N], under the hood anywhere. A hostile implementation could implement std::array<T, 2> for example as a struct { T first, second; }; From this follows that std::array is *not* in general (at least as far as the Standard is concerned) pointer-interconvertible with a plain array or any other unrelated type.
In order to make this work, we need a new language facility along the lines of P1912 <https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1912r1.pdf>, which is a paper that I unfortunately didn't have time to work on further, but I am hoping to find some time in the future to pick it up again.
Cheers,
Timur
> On 17 Aug 2023, at 13:30, Lénárd Szolnoki via Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> On Thu, 2023-08-17 at 09:41 +0100, Gašper Ažman wrote:
>> Yes, what Lénárd said, unless you actually need to treat it as a
>> std::array, in which case I'm still interested in the answer to my
>> question.
>>
>
> Pointer interconvertibility still only allows to convert and access
> through the pointer if the object of the corresponding type is there.
> For some T std::array<T, N> should be implicit-lifetime, so you might
> be able to do create the object with start_lifetime_as (or self-
> memmove, if applicable). I'm not sure if accessing the elements through
> a reference or name to the original C array is defined after this
> operation though.
>
>>
>> On Thu, Aug 17, 2023, 09:27 Lénárd Szolnoki via Std-Proposals
>> <std-proposals_at_[hidden]> wrote:
>>> On Thu, 2023-08-17 at 09:12 +0100, Frederick Virchanza Gotham via
>>> Std-
>>> Proposals wrote:
>>>> On Wed, Aug 16, 2023 at 11:08 PM Jason McKesson wrote:
>>>>>>
>>>>>> std::array arr = {
>>>>>> something1,
>>>>>> something2,
>>>>>> something3,
>>>>>> };
>>>>>>
>>>>>> also works.
>>>>>
>>>>> That deduction guide has been there since C++17, so it's
>>>>> standard.
>>>>> Assuming of course that all of those are of the same type.
>>>>
>>>>
>>>> I'm looking through cppreference.com here and I don't see a
>>>> function
>>>> something like:
>>>>
>>>> template< typename T, std::size_t len >
>>>> std::array<T,len> &pretend_is_std_array( T (&arg)[len] ); //
>>>> returns
>>>> a reference
>>>>
>>>> What are we supposed to do if we want to treat a C-style array as
>>>> though it's an std::array? Should we just reinterpret_cast?
>>>
>>> No, use std::span. C arrays, std::array and even std::vector and
>>> subranges of these are compatible with std::span.
>>>
>>> Or if you are writing a template then use contiguous_range, if you
>>> have
>>> to make use of contiguity.
>>>
>>> Cheers,
>>> Lénárd
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Received on 2023-08-17 10:50:36