Unwrap variadic template into vararg of pointers of the same types

ryuukk_ ryuukk.dev at gmail.com
Mon Jul 11 22:34:13 UTC 2022


On Friday, 8 July 2022 at 12:48:20 UTC, Paul Backus wrote:
> On Friday, 8 July 2022 at 12:20:13 UTC, ryuukk_ wrote:
>> The problem when i try to introduce variadic template, is i 
>> can't seem to understand how to unwrap the parameter as 
>> pointer type T -> T*
>>
>>
>> ```D
>> struct Includes(Args...) { alias args = Args; }
>>
>> void view_it(Includes)(void function(entity_t, Includes.args* 
>> ) cb)
>> {
>>     // do stuff
>> }
>>
>> ```
>>
>> I get the following:
>>
>> ```Error: cannot have pointer to `(EEntityRemoved)```
>
> You can use [`std.meta.staticMap`][1] to make each type into a 
> pointer individually:
>
> ```d
> import std.meta;
>
> alias Args = AliasSeq!(int, string, double);
> struct Includes { alias args = Args; }
> struct entity_t {}
>
> alias PointerTo(T) = T*;
>
> void view_it(void function(entity_t, staticMap!(PointerTo, 
> Includes.args) ) cb)
> {
>     // do stuff
> }
> ```
>
> However, this will probably not work very well in your original 
> code where `view_it` is a template, because the presence of 
> `staticMap` in the parameter list will prevent the compiler 
> from automatically deducing the template parameters.
>
> So, in this case, I think a better solution is to make the type 
> of the callback fully generic, and use a template constraint to 
> enforce your requirements:
>
> ```d
> import std.meta: allSatisfy;
> import std.traits: isPointer;
>
> void view_it(Callback)(Callback cb)
>     if (
>     	is(Callback == void function(entity_t, Args), Args...)
>         && allSatisfy!(isPointer, Args)
> 	)
> {
>     // do stuff
> }
> ```
>
> [1]: http://phobos.dpldocs.info/std.meta.staticMap.html

Looks like my previous message did not get sent, i might have 
pressed on the wrong button

Thanks for the snippet, i ended up copying the code from the 
imports, since importing the modules tanks my compile speed

Although i'd prefer not rely on generic callback since i want to 
keep code completion, so i'll have to dig a little more, but 
that's for later, the solution works for now


More information about the Digitalmars-d-learn mailing list