Reimundo Heluani
Reimundo Heluani

Reputation: 978

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)?

Upvotes: 1

Views: 41

Answers (1)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145269

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.

Upvotes: 3

Related Questions