-1

I last used C++ in 2003, and am trying to revisit, and learn the new stuff. Have been working in Java for a while, and just learning its Lambda syntax. Saw this example in C++, and wondering if someone could explain it:

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int i = 42;
char code = 'c';

[=, &i]() mutable
{
    i++;
    code = 'd';
    std::cout << "i: " << i << "code " << code << std::endl;
}();


void wait()
{
    cin.clear();
    cin.ignore();
    cin.get();
}

int main()
{
    std::cout << "i: " << i << "code " << code << endl;


    // hold output window open
    wait();
    return 0;
}

Is this new lambda syntax for operator= overload? I'd write a test function, but am not sure how to invoke it.

frododot
  • 127
  • 2
  • 16
  • No it's not. Have you tried running it and observing what happen? – litelite Aug 21 '17 at 20:25
  • No it is not. You should do some reading on lambdas. As a sidenote, this `<< cout::endl;` will not compile. – Ron Aug 21 '17 at 20:27
  • https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11 – Pierre Baret Aug 21 '17 at 20:28
  • I understand the keywords (reference, mutable, etc), but as I am new to this specific syntax in C++, I am unsure how to even execute this method. Surprised at the instantaneous downvote (which implies the answer is obvious). Stating "No it is not" does not assist. What is the 1-line needed to test the method? If I can see how to invoke it, then I am off an running. Kind Regards,... – frododot Aug 21 '17 at 20:45
  • @Pierre Baret: this portion of link helped: You can capture by both reference and value, which you can specify using & and = respectively: [&epsilon] capture by reference [&] captures all variables used in the lambda by reference [=] captures all variables used in the lambda by value [&, epsilon] captures variables like with [&], but epsilon by value [=, &epsilon] captures variables like with [=], but epsilon by reference – frododot Aug 21 '17 at 21:01

4 Answers4

0

The = means it will capture all variables by value, &i specifies that i must be captured by reference.

The mutable means it can change input parameters values. And as there are none this has no effect at all.

Ron
  • 14,674
  • 4
  • 34
  • 47
mamayo
  • 67
  • 1
  • 7
0

[] before the lamda is the capture list for the lambda. If it says [=], it captures all automatic variables used in the lambda by copy. In your case this will mean i is captured by reference and code(the other automatic variable used inside the lambda) is captured by copy.

The mutable means that the variables are captured mutably, so the lambda can change the captured variables. But the lambda here doesn't change any captured variables anyways ...

Malice
  • 1,457
  • 16
  • 32
0

I can't reply back to comments as I still don't have 50 rep points (my previous answer was automatically moved to comments as it was just a link).

I found a simple way of testing lambda expressions but I quite don't understand the purpose of your code sample so I am posting it raw here. I hope it helps, it is from the Book SFML Game Development, and uses std::functions

int add(int a, int b) { return a + b };
// Assign a lambda expression to a function
std::function<int(int, int)> adder1 = [] (int a, int b) { return a + b; };

int sum = adder1(3, 5); // same as add(3, 5)
Pierre Baret
  • 1,773
  • 2
  • 17
  • 35
0

Thanks to all for your assistance. Found detailed explanation here, : section "How are Lambda closures implemented"

frododot
  • 127
  • 2
  • 16