Date: Mon, 21 Sep 2020 22:46:46 +0100
Would this work?
https://godbolt.org/z/WxoG4j
std::extent_v should work so long as the array has not been implicitly
converted to a pointer. However, I personally would recommend using a
std::array, if possible, given that it's easy to get its size.
https://godbolt.org/z/shW34f
Alternatively, perhaps there's another way to refactor this? I'm not sure
how a std::pair could be needed here, other than to store the size.
On Mon, 21 Sep 2020 at 21:45, Ramkumar Ramachandra via Std-Proposals <
std-proposals_at_[hidden]> wrote:
> There's however a problem doing that with my code:
>
> ```
> template <typename T>constexpr std::pair<TableEntry<i32 T::*> *, size_t> M;
>
> template <>
> constexpr inline auto M<Page> = std::make_pair(PageTable,
> std::extent_v<decltype(PageTable)>);
> template <>
> constexpr inline auto M<Layer> = std::make_pair(LayerTable,
> std::extent_v<decltype(LayerTable)>);
> template <>
> constexpr inline auto M<Line> = std::make_pair(LineTable,
> std::extent_v<decltype(LineTable)>);
> template <>
> constexpr inline auto M<Point> = std::make_pair(PointTable,
> std::extent_v<decltype(PointTable)>);
> ```
>
> If I factor out the std::extent_v<decltype(...)> into an auto template, it
> picks the incorrect type (a pointer, as opposed to a sized-array), and I
> always get size 0. Any suggestions?
>
> R.
>
> On Mon, Sep 21, 2020 at 10:38 PM Ramkumar Ramachandra <artagnon_at_[hidden]>
> wrote:
>
>> Ah, thanks!
>>
>> R.
>>
>> On Mon, Sep 21, 2020 at 10:36 PM Jason McKesson via Std-Proposals <
>> std-proposals_at_[hidden]> wrote:
>>
>>> On Mon, Sep 21, 2020 at 3:29 PM Ramkumar Ramachandra via Std-Proposals
>>> <std-proposals_at_[hidden]> wrote:
>>> >
>>> > I would like to propose:
>>> >
>>> > ```
>>> > template <typename T = decltype(V), T V>
>>> > constexpr auto Sz = std::extent_v(V);
>>> > ```
>>>
>>> That's a circular definition. The compiler can't know what `V` is
>>> without knowing what `T` is and vice-versa.
>>>
>>> Equally importantly, I'm pretty sure C++17 gives us similar
>>> functionality as follows:
>>>
>>> ```
>>> template<auto V>
>>> constexpr auto Sz = std::extent_v<decltype(V)>;
>>> ```
>>> --
>>> Std-Proposals mailing list
>>> Std-Proposals_at_[hidden]
>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>
>> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
https://godbolt.org/z/WxoG4j
std::extent_v should work so long as the array has not been implicitly
converted to a pointer. However, I personally would recommend using a
std::array, if possible, given that it's easy to get its size.
https://godbolt.org/z/shW34f
Alternatively, perhaps there's another way to refactor this? I'm not sure
how a std::pair could be needed here, other than to store the size.
On Mon, 21 Sep 2020 at 21:45, Ramkumar Ramachandra via Std-Proposals <
std-proposals_at_[hidden]> wrote:
> There's however a problem doing that with my code:
>
> ```
> template <typename T>constexpr std::pair<TableEntry<i32 T::*> *, size_t> M;
>
> template <>
> constexpr inline auto M<Page> = std::make_pair(PageTable,
> std::extent_v<decltype(PageTable)>);
> template <>
> constexpr inline auto M<Layer> = std::make_pair(LayerTable,
> std::extent_v<decltype(LayerTable)>);
> template <>
> constexpr inline auto M<Line> = std::make_pair(LineTable,
> std::extent_v<decltype(LineTable)>);
> template <>
> constexpr inline auto M<Point> = std::make_pair(PointTable,
> std::extent_v<decltype(PointTable)>);
> ```
>
> If I factor out the std::extent_v<decltype(...)> into an auto template, it
> picks the incorrect type (a pointer, as opposed to a sized-array), and I
> always get size 0. Any suggestions?
>
> R.
>
> On Mon, Sep 21, 2020 at 10:38 PM Ramkumar Ramachandra <artagnon_at_[hidden]>
> wrote:
>
>> Ah, thanks!
>>
>> R.
>>
>> On Mon, Sep 21, 2020 at 10:36 PM Jason McKesson via Std-Proposals <
>> std-proposals_at_[hidden]> wrote:
>>
>>> On Mon, Sep 21, 2020 at 3:29 PM Ramkumar Ramachandra via Std-Proposals
>>> <std-proposals_at_[hidden]> wrote:
>>> >
>>> > I would like to propose:
>>> >
>>> > ```
>>> > template <typename T = decltype(V), T V>
>>> > constexpr auto Sz = std::extent_v(V);
>>> > ```
>>>
>>> That's a circular definition. The compiler can't know what `V` is
>>> without knowing what `T` is and vice-versa.
>>>
>>> Equally importantly, I'm pretty sure C++17 gives us similar
>>> functionality as follows:
>>>
>>> ```
>>> template<auto V>
>>> constexpr auto Sz = std::extent_v<decltype(V)>;
>>> ```
>>> --
>>> Std-Proposals mailing list
>>> Std-Proposals_at_[hidden]
>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>
>> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
Received on 2020-09-21 16:46:58