[Cs3] lambda functions

Mikhail Nesterenko mikhail at cs.kent.edu
Wed Jun 25 19:47:43 EDT 2014


CS3 students,

First, if the capture list is empty, the variables should not be
implicitly captured from the environment (outside scope):


  int myvar=55;
  auto myFuncPointer = [](){cout << myvar;};

should NOT compile. However, clang/gcc apparently allow it. I have not
checked MSVS.

Now, we missed a syntactic feature of lambda functions: you can
specify a return value as follows:

  auto myFuncPointer = []() -> int {return 55;};

However, this specification is optional as the compiler will
automatically deduce the return value type from the type of the
expression in the return statement. Note that this deduction happens
only if there is a single return statement in the lambda.

Note also that the explicit parameter list parentheses are also
optional. So the below code is legal:

 auto myFuncPointer = [] {cout << "Hello, World!";};

I updated the slides and will talk more about it on Friday.

Thanks,
-- 
Mikhail


More information about the cs3 mailing list