This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 268358 - C++14: Variable captured with initializer by a lambda is unable to be resolved
Summary: C++14: Variable captured with initializer by a lambda is unable to be resolved
Status: NEW
Alias: None
Product: cnd
Classification: Unclassified
Component: Code Model (show other bugs)
Version: 8.2
Hardware: PC Linux
: P3 normal with 1 vote (vote)
Assignee: petrk
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-10-05 16:21 UTC by Zcool31
Modified: 2017-03-31 18:35 UTC (History)
1 user (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
code that demonstrates the same or related issue (278 bytes, text/x-c++src)
2017-03-14 19:54 UTC, vertago1
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Zcool31 2016-10-05 16:21:13 UTC
The following C++14 code is valid, but the IDE does not recognize the captured variable.

void foo() {
  int bar = 1;
  auto baz = [quux = bar]()->void { // unable to resolve identifier quux
    static_cast<void>(quux); // unable to resolve identifier quux
  };
}

Behavior is the same with multiple captures:

void foo2() {
  int bar1 = 1;
  int bar2 = 2;
  auto baz = [quux1 = bar1, quux2 = bar2]()->void {
      // unable to resolve identifier quux1
      // unable to resolve identifier quux2
  };
}
Comment 1 soldatov 2016-10-25 09:28:24 UTC
#include <iostream>

using namespace std;

void foo() {
    int bar = 1;
    auto lambda = [&bar = bar]() {
        bar = 10;
        cout << bar << endl;
        return bar;
    };
    lambda();
    cout << bar << endl;
}

int main() {
    foo();
    return 0;
}

In "return bar;" line If I hold Ctrl key and click on 'bar' identifier, then cursor jumps to "int bar = 1;" line instead of "auto lambda = [&bar = bar]() {"
Comment 2 vertago1 2017-03-14 19:54:43 UTC
Created attachment 163836 [details]
code that demonstrates the same or related issue

// This code also compiles fine but code assistance handles incorrectly.
// It may not be the same issue, but probably should be fixed at the same time.

#include <functional>
#include <iostream>

struct wrapper {
  std::function<int(const std::string&)> callback;
};

int main() {
  wrapper lambda_instance{[](const std::string& msg) -> int {
    std::cout << msg <<"\n";
  }};
  return lambda_instance.callback("Hello World!");
}
Comment 3 petrk 2017-03-31 18:35:29 UTC
Yes, the latter problem related to the uniform initialization syntax. Without it "msg" is resolved:


int main() {
  wrapper lambda_instance = [](const std::string& msg) -> int {
    std::cout << msg <<"\n";
  };
  return lambda_instance.callback("Hello World!");
}


I think, I'll file a separate bug for this later.