C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Iterating over a parameter pack for...

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Tue, 5 Dec 2023 12:46:30 +0100
wt., 5 gru 2023 o 12:19 Frederick Virchanza Gotham via Std-Proposals
<std-proposals_at_[hidden]> napisaƂ(a):
>
> On Tue, Dec 5, 2023 at 10:02 AM Marcin Jaczewski wrote:
> >
> > switch (x)
> > {
> > template for (constexpr auto a : list)
> > {
> > // this line is effective copy pasted multiple times
> > // with different `a` from each iteration of `template for`
> > case a: return foo<a>();
> > }
> > }
>
>
> What are you trying to do there in that code? Is 'x' a compile-time
> constant integer? Is 'list' a pack of types, or a pack of variables?
> Could you please write a full template function?
>

`x` is normal runtime value, `list` is compile time list.
`switch` is one of few places that can't interact with `...` packs.
Normal approach like recursion is dangerous when compiler hit
inline limits and it will create multiple nested calls that will
tank performance of hot spots, like the main loop of binary code evaluator
or deserialization.

> Anyway it would be interesting to have 'switch typename', which we
> could combine with a "for typename..." as follows:
>
> template<typename... Ts>
> consteval bool AtLeastOneTypeIsSignedShortOrSignedInt(void) noexcept
> {
> for typename... ( T : Ts )
> {
> switch typename ( T )
> {
> case typename int :
> case typename short : return true;
> }
> }
>
> return false;
> }

I do not see benefits for switch by type, only the real benefit
of `switch` is better chance of optimal runtime over `if else` chain.
There is no concern like this during compilation time.

Show case where you can't use a couple of `constexpr if
(std::is_same_v<T, int>)`.

> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2023-12-05 11:46:42