* Sarang:
I was planning to write a recursive version of prime numbers using lambda
but not sure if I can use lambda's recursively:
You need a fixpoint combinator. I tried to write one:
template<typename F, typename... Args>
typename std::result_of<F>::type
fix(F f, Args&&... args)
{
auto fix_f = [&](Args&&... args1) -> typename std::result_of<F>::type {
return f(fix_f, std::forward<Args>(args1)...);
};
return fix_f(std::forward<Args>(args)...);
}
bool is_prime_r(int num)
{
auto prime_rec = [](std::function<int(int, int)> rec,
int num, int den) -> bool
{
if(den == 1 )
{
return true;
}
else if( num % den == 0 )
{
return false;
}
else
{
return rec(num, den -1);
}
};
return fix(prime_rec, num, num - 1);
}
But GCC 4.6 doesn't like it, either:
| error: no matching function for call to 'fix(is_prime_r(int)::<lambda(std::function<int(int, int)>, int, int)>&, int&, int)'
| note: candidate is:
| note: template<class F, class ... Args> typename std::result_of<F>::type fix(F, Args&& ...)
Is the code above supposed to compile with a C++0X compiler?
1) You are using std::result_of incorrectly, you must specify the arguments of the functor F.
2) Within function fix you are using auto as type-specifier in a variable declaration incorrectly, because you are referring to the name of the declared variable (fix_f) within its initializer (7.1.6.4p3 as quoted in my other reply).
[ comp.lang.c++.moderated. First time posters: Do this! ]