5

Can you explain which type of L in this context. In other words what type I can use instead auto keyword?

int main(){
 int x=0;
 auto L = [x] (int y)->bool{
   return x>y;
 };
  return 0; 
}
Stepan Loginov
  • 1,667
  • 4
  • 22
  • 49

2 Answers2

7

There is nothing in C++11 which you could use instead of auto in this context that would mean the exact same type. That's because the type of each lambda expression is a unique, unnamed type. Quoting C++11 5.1.2/3:

The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union class type — called the closure type — whose properties are described below. ...

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
1

You can use std::function instead of `auto, but you may not want to.

This article explains in more detail:

The basic principle behind auto is that the compiler knows the type … but you either can’t describe it or don’t want to. There is one primary use-case where you cannot name the type – with lambdas.

The article then notes how you can use a std::function instead, but with a run-time penalty.

doctorlove
  • 18,872
  • 2
  • 46
  • 62