4

Shouldn't this be possible with C++11?
With the current clang compilers (Xcode 5 on OS X 10.8) it fails to compile:

std::max_element(group->GetComponents().begin(), group->GetComponents().end(),
                 [](auto a, auto b) { return a.length > b.length; });

The error message is: Stuff.cp:68:40: 'auto' not allowed in function prototype

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ATV
  • 4,116
  • 3
  • 23
  • 42
  • 3
    Function argument types cannot be `auto`. There is no way to deduce the type. – R Sahu Feb 21 '14 at 20:47
  • possible duplicate of [Argument type auto deduction and anonymous lambda functions](http://stackoverflow.com/questions/5712826/argument-type-auto-deduction-and-anonymous-lambda-functions) – DarkWanderer Feb 21 '14 at 20:48
  • Also: [How does generic lambda work in C++14?](https://stackoverflow.com/questions/17233547/how-does-generic-lambda-work-in-c14) –  Feb 21 '14 at 20:49
  • `Shouldn't this be possible with C++11`? That's what [C++11 and the Lack of Polymorphic Lambdas - Why?](https://stackoverflow.com/questions/4643039/c11-and-the-lack-of-polymorphic-lambdas-why) addresses. –  Feb 21 '14 at 20:49
  • 1
    @RSahu In this specific context (template algorithm) it could be deduced from the type of the collection, right? – ATV Feb 21 '14 at 20:51
  • @ATV the link from DarkWanderer's comment provides a nice explanation. – R Sahu Feb 21 '14 at 20:56

1 Answers1

1

In C++1y you have generic lambdas, so the syntax will compile in clang 3.5. The lambda will look like this:

class /* unnamed */
{
public:
    template<typename T>
    T operator () (T a) const { return a; }
};