6

Hi stack overflow community!

I wonder why this code doesn't compile with C++17

std::unordered_map<int, int> a;
for (const auto& [k, v]: a) {
  auto l = [k] () {};
}

with error "'k' in capture list does not name a variable".

But this compiles good

std::unordered_map<int, int> a;
for (const auto& [k, v]: a) {
  auto l = [k = k] () {};
}

What is k in [k, v] if it is not a variable?

finisterra
  • 71
  • 1
  • 3
  • I stumbled on the exact same problem! According to https://stackoverflow.com/questions/46114214/lambda-implicit-capture-fails-with-variable-declared-from-structured-binding a structure binding introduces names but not varaibles? It also seems your fix is the appropriate one, assigning a variable k to the named value k? I hope future C++ fixes this confusion :) – Kjell-Olov Högdahl Jan 03 '22 at 11:15

0 Answers0