C++ Logo

std-proposals

Advanced search

Re: Meta types

From: Михаил Найденов <mihailnajdenov_at_[hidden]>
Date: Fri, 7 Feb 2020 21:56:26 +0200
On Fri, Feb 7, 2020 at 8:43 PM Jake Arkinstall <jake.arkinstall_at_[hidden]>
wrote:

> On Fri, 7 Feb 2020, 18:12 Михаил Найденов, <mihailnajdenov_at_[hidden]>
> wrote:
>
>>
>>
>> On Fri, Feb 7, 2020 at 7:08 PM Lee Howes <xrikcus_at_[hidden]> wrote:
>>
>>> Well the idea would be that you could unroll it too:
>>> for co_await(auto [key, value] : dictionary) {
>>> foo(key, value);
>>> }
>>> (I forget what we intended for for co_await syntax, but it's not in
>>> there yet anyway so let's go with it)
>>>
>>> The code would end up instantiating foo over all the key/value type
>>> combinations. The coroutine callback logic for dictionary iteration would
>>> yield each key/value pair by callback directly without any variant involved.
>>>
>>> This is all speculation though, because actually implementing this and
>>> convincing people that the reader will not be confused is still a bit of a
>>> step.
>>>
>>
>> This is template for, I believe.
>>
>
>

> For the case of iterating over a static dictionary, template for would do
> well.
>
> It doesn't, however, solve the issue of iterating over types yielded based
> on runtime values, such as a sequence of events generated by an input
> string. It would instead require the sequence of types to be known at
> compile time. A template for in this context would allow branching to
> different instantiation of the for block depending on the type generated on
> each iteration.
>

yeah, got ticked, because dictionary was a variable.

In any case, coroutines aside, if just the indirection is the issue, we
should really see what optimization will be possible with Patter Matching +
(language-or-not) variant.

Consider for example

variant<uint, float> halve(unsigned i){
    if(i % 2 == 0){
        return i / 2; // unsigned path
    }else{
        return i * 0.5; // double path
    }
}

void caller(uint i) {
  inspect halve(i) Numeric x => /*do work*/;
}

If all cases are handled, and if halve is inline, the compiler will be able
to transform the caller

void caller(uint i) {
    if(i % 2 == 0){
        unsigned x = i / 2; /*do work*/ ;
    }else{
        float x = i * 0.5; /*do work*/ ;
    }
}

The fact that the compiler knows about visitation naively will surely lead
to some powerful new optimization possibilities, without additional
language machinery.

Received on 2020-02-07 13:59:17