I would like to declare a lambda function with exactly N parameters, where N is a template argument. Something like...
template <int N>
class A {
std::function<void (double, ..., double)> func;
// exactly n inputs
};
I could not think of a way to do this with the metafunction paradigm.
A meta
template
that takes a template, a count, and a type, and invokes the template withN
copies of the type:Now, we use it:
And we test it:
and win.
What exactly you use the
double, double, double
for is completely up to you in the above system. You can have a lambda that you initialize astd::function
with, for example.You can pack up the
double, double, double
into atemplate<class...>struct type_list{};
so you can pass it as one argument to anothertemplate
, then specialize to unpack it.A
repeat_type
that has less recursion for largeN
:For large
N
, the above can significantly reduce compile times, and deal with overflowing thetemplate
stack.For completeness, here is a solution without recursion:
See it live on Coliru
You can't do this directly.
You can do something like this
You can write a template
n_ary_function
with a nested typedeftype
. This type can be used as follows:The following code fragment contains the definition of
n_ary_function
: