Thread overview
equivalent of std.functional.partial for templates?
Aug 11, 2021
Paul Backus
Aug 11, 2021
Paul Backus
Aug 11, 2021
jmh530
August 11, 2021

I have a template function like this:

auto foo(T, Args...)(Args args) {...}

If I try to bind the T only, and produce a partial template function which can accept any number of parameters, but has T already specified, I get an error, because instantiating foo!T means Args is length 0.

I was surprised not to see something in std.meta that could create this for me. In std.functional there is partial (which bizarrely only allows one parameter to be specified), but I don't see an equivalent for template parameters.

For functions, it seems IFTI can't see through to infer the template parameters of the alias, but you can have a function wrapper:

template bindFirst(alias F, Args...)
{
   // this doesn't work
   // alias bindFirst(Args2...) = F!(Args, Args2);
   // but this does
   auto bindFirst(Args2...)(Args2 args2) { return F!(Args)(args2); }
}

alias bf = bindFirst!(foo, int);
bf("hello", 5); // ok

It would be nice if the alias worked, but I don't know what's involved to make IFTI work there. I know I can alter foo to be a template within a template. Perhaps that's the right answer. However, this doesn't help for functions already written that I can't alter.

Any ideas on how to solve this? I know my function solution is not very flexible...

-Steve

August 11, 2021

On Wednesday, 11 August 2021 at 14:00:33 UTC, Steven Schveighoffer wrote:

>

I have a template function like this:

auto foo(T, Args...)(Args args) {...}

If I try to bind the T only, and produce a partial template function which can accept any number of parameters, but has T already specified, I get an error, because instantiating foo!T means Args is length 0.

https://phobos.dpldocs.info/std.meta.ApplyLeft.html

August 11, 2021

On Wednesday, 11 August 2021 at 14:03:50 UTC, Paul Backus wrote:

>

On Wednesday, 11 August 2021 at 14:00:33 UTC, Steven Schveighoffer wrote:

>

I have a template function like this:

auto foo(T, Args...)(Args args) {...}

If I try to bind the T only, and produce a partial template function which can accept any number of parameters, but has T already specified, I get an error, because instantiating foo!T means Args is length 0.

https://phobos.dpldocs.info/std.meta.ApplyLeft.html

Should have read further--this does not work with template functions due to issue 1807. My mistake.

August 11, 2021

On Wednesday, 11 August 2021 at 14:08:59 UTC, Paul Backus wrote:

>

On Wednesday, 11 August 2021 at 14:03:50 UTC, Paul Backus wrote:

>

On Wednesday, 11 August 2021 at 14:00:33 UTC, Steven Schveighoffer wrote:

>

I have a template function like this:

auto foo(T, Args...)(Args args) {...}

If I try to bind the T only, and produce a partial template function which can accept any number of parameters, but has T already specified, I get an error, because instantiating foo!T means Args is length 0.

https://phobos.dpldocs.info/std.meta.ApplyLeft.html

Should have read further--this does not work with template functions due to issue 1807. My mistake.

So first, I though ApplyLeft would work, but I convinced myself it was focused on applying the template to arguments individually, but that's just the example used. ApplyLeft is exactly what I'm looking for, but as you said (and as my example shows), it doesn't work in this case. It would be nice to have something that would work (or make ApplyLeft work via a compiler change).

Thanks for the nudge!

-Steve

August 11, 2021

On Wednesday, 11 August 2021 at 14:08:59 UTC, Paul Backus wrote:

>

[snip]

Should have read further--this does not work with template functions due to issue 1807. My mistake.

Looks like that strengthens the case for moving forward with DIP1023 (or something more general).