On 2/8/19 03:51, Öö Tiib wrote:
> On Thursday, 7 February 2019 18:46:53 UTC+2, Marcel Mueller wrote:
>> If I have a array variable
>>
>> std::array<int,5> data;
>>
>> and a function taking an array reference
>>
>> void foo(int (&arr)[5]);
>>
>> how to pass the content of data to this function without a hard cast?
>>
>> All the methods of std::array return only raw pointers without the
>> required size in the type.
>>
>>
>> OK, I can write a template helper function to do a safe conversion from
>> std::array<T,N>& to T(&)[N]. But this can no longer be constexpr because
>> of the cast.
>
> Write helper function template that takes reference to
> implementation-specific member (like "__elems_" in libcpp)
> directly. If you target multiple platforms then choose what
> it's name is with preprocessor. That member is required to
> exist and to be public by standard.
Citation, please? 26.3.7.1p3 doesn't list any public data members for
std::array<>, as far as I can see, and neither do any of the general
container requirements (tables 83, 84, 85, 86, and 87).
>> Why does std::array<T,N>::data not return T(&)[N] which implicitly
>> converts to T* if the length information is not needed? The internal
>> storage is of type T[N] anyway.
>
> It is issue 930 with status "not a defect".
>
https://cplusplus.github.io/LWG/issue930
> So it won't change anytime soon and therefore I would go with what
> I suggested above.
That issue mentions std::array::elems, which was marked "for exposition
only". "for exposition only" means precisely that - it does NOT mandate
that std::array be implemented in that manner, only that it have the
same observable behavior as if it did so. The identifier "elems" is not
present anywhere in the n4659.pdf, the latest draft version of the C++
standard that I have on my desktop machine, essentially equivalent to
C++2017. Perhaps the committee decided that it was misleading to explain
the behavior of std::array in terms of elems?