79604858

Date: 2025-05-03 15:46:58
Score: 1.5
Natty:
Report link

Why compiler can’t understand in foo1 that all I need it to capture f by value since it is the only variable in used in lambda? Why this must be explicitly stated as in foo2?

You need to declare variable lambda in order to capture.

Snippet:

void foo1() {
    double d = 1.0;
    auto lambda = [=, &d]() { d = double(f); };
    lambda();  
}

void foo2() {
    double d = 1.0;
    auto lambda = [f=f, &d]() { d = double(f); };
    lambda();  
}
Reasons:
  • Blacklisted phrase (0.5): I need
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): Why
  • Low reputation (0.5):
Posted by: Adios Gringo