C++ Logo

std-proposals

Advanced search

Re: [std-proposals] C-style array specialization for std::get

From: Nikolay Mihaylov <nmmm_at_[hidden]>
Date: Tue, 1 Mar 2022 19:20:35 +0200
Sure and since is template parameter, it will still checked for non
constexpr version. E.g. kind of.

However I still think we can have something similar to pointers too.

One more minor point defending pointer version - unless optimized away,
this will generate loooots of functions. I love templates, but it will
bloat the code.

Nick.

On Tuesday, March 1, 2022, DBJ via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> That static_assert will kick in only if the function is called:
>
> #include <assert.h>
> #include <cstdlib>
>
> template <std::size_t Idx, typename T, std::size_t N>
> constexpr T& get(T (&arr)[N]) noexcept {
> static_assert(Idx < N, "Index out of bounds");
> return arr[Idx];
> }
>
> int main(void) {
> constexpr int arr[99] = {1, 2, 3, 4};
> static_assert(get<2>(arr) == 3);
>
> typedef decltype(&get<99>(arr)) wtf;
> wtf p = &get<2>(arr); // works
> }
>
> C++20 requires variant is better.
>
> DBJ
>
> On Tue, 1 Mar 2022 at 17:39, Arthur O'Dwyer via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>> On Tue, Mar 1, 2022 at 11:34 AM Barry Revzin <barry.revzin_at_[hidden]>
>> wrote:
>>
>>> On Tue, Mar 1, 2022 at 9:59 AM Arthur O'Dwyer via Std-Proposals <
>>> std-proposals_at_[hidden]> wrote:
>>>
>>>> On Tue, Mar 1, 2022 at 10:13 AM Nikolay Mihaylov via Std-Proposals <
>>>> std-proposals_at_[hidden]> wrote:
>>>>
>>>>> Once again, why do we need the size?
>>>>> Shall we check the size?
>>>>> Via static_assert?
>>>>> During runtime?
>>>>>
>>>>
>>>> In the proposed std::get for C arrays, AIUI, the size would be part of
>>>> the signature. It would look like this:
>>>>
>>>> template<size_t X, class T, size_t N> requires (X < N)
>>>> constexpr T& get(T (&arr)[N]) noexcept {
>>>> return arr[X];
>>>> }
>>>>
>>>
>>> No, it wouldn't. Paulo's email contained the correct implementation:
>>>
>>> template <std::size_t Idx, typename T, std::size_t N>
>>> constexpr T& get(T (&arr)[N]) noexcept
>>> {
>>> static_assert(Idx < N, "Index out of bounds");
>>> return arr[Idx];
>>> }
>>>
>>> This would match what std::get does for std::array, std::pair, and
>>> std::tuple.
>>>
>>
>> Oh, gross. If it's not going to be SFINAE-friendly, then I don't
>> particularly have an opinion what it does.
>>
>> –Arthur
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>
>

Received on 2022-03-01 17:20:39