Storing a lambda alongside type-erased data
home at home.com
Fri Sep 9 02:32:28 UTC 2022
On Thursday, 8 September 2022 at 15:02:13 UTC, Paul Backus wrote:
> On Thursday, 8 September 2022 at 03:18:08 UTC, Ali Çehreli
>> I looked at how std.variant.VariantN prints the correct type
>> and failed to understand the magic there. :(
>> Then I came up with storing a lambda that is created when the
>> exact type is known. The following simple variant can carry
>> arbitrary set of data because the data is provided as sequence
>> template parameters (aka variadic).
> This is actually pretty much exactly what VariantN does, except
> instead of storing a pointer to a lambda, it stores a pointer
> to an instance of a template function.
> The member variable `fptr`  is the equivalent of your
> `dataToStr`. It stores a pointer to an instance of the
> `handler` template . Whenever a new value is assigned to the
> VariantN, `fptr` is updated to point to the template instance
> corresponding to the new value's type .
My implement is similar but a pointer to template struct with
various functions. The advantage is that you can add various
attributes to those functions
More information about the Digitalmars-d-learn