lundi 4 mars 2019

How to make a recursive lambda function that takes 2 arguments not 1?

I already know how to make a recursive lambda function that take one argument like calculating factorial of a number, but I try to make a recursive power function using lambda (as a practice), but taking 2 arguments in the function caused errors

this the code :

std::function <int(int)> power = [&](int a, int n)
{
    return (n<=1) ? a : a*power(a, n-1);
};

this line return (n<=1) ? a : a*power(a, n-1); gives these errors :

error: no match for call to '(std::function) (int&, int)'|

note: candidate: _Res std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = int; _ArgTypes = {int}]|

note: candidate expects 1 argument, 2 provided|

Aucun commentaire:

Enregistrer un commentaire