|
|
|
|
|
by bmastenbrook
5648 days ago
|
|
Yes, it'd work with 0x lambdas; however, the situations where this occurs in C are much less common in C++. Consider the "max" example given, as written in C++: template <typename T> T max(T a, T b)
{
return (a > b) ? a : b;
} int max_ints(int a, int b)
{
return max(a, b);
} Of course this only works if "a" and "b" have the same type. 0x provides a solution for the situation where they don't: template <typename Ta, typename Tb> auto max(Ta a, Tb b) -> decltype((a > b) ? a : b)
{
return (a > b) ? a : b;
} int max_int_char(int a, char b)
{
return max(a, b);
} |
|
For example:
Which, of course, works fine if you call it as a statement. But it doesn't work if you want to somehow "return" the value of "y" from that: there's no way to embed that kind of loop into an expression.He showed an example of using Apple's Blocks to solve that, can you do the same with C++0x lambdas?