Unwrap variadic template into vararg of pointers of the same types
Paul Backus
snarwin at gmail.com
Fri Jul 8 12:48:20 UTC 2022
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
More information about the Digitalmars-d-learn
mailing list