Reimundo Heluani Reimundo Heluani - 9 months ago 27
C++ Question

template to specialize a function's parameter

I have a function

int f (int x, int y)
which needs to call itself plenty of times, with one of its parameters fixed, as in

int f(int x, int y) {
int i = f(z,y);

Is there any way of defining via a template a function
int g (int x)
such that
g(z) := f(z,y)
so that the above call would have been
int i = g(z)


You can just define that without any templating,

auto f( int x, int y )
    -> int
    auto g = [=]( int z ) -> int { return f( z, y ); };
    // ...
    int i = g( z );

You can omit the -> int result type specification for g if you want.

Disclaimer: code not touched by compiler's hands.