1

I don't know how to better phrase my question, since it's the first time encountering this. Basically I have a function and inside this function an anonymous function is called(a lambda).

std::ostream& Print(std::ostream& os){
/*
    some code
*/
    classMember.doSmth([&](std::size_t num){
           auto [var1, var2] = classMember.funcThatReturnsATupple(num) // <- this I don't understand
    });
}

void doSmth(const std::function<void(std::size_t num)>& function) const{
    //does stuff
}

var1 and var2 are not declared anywhere in Print(). I'm only allowed to declare them in doSmth(), but i don't quite understand what they are, nor how they work. Is the following somewhat equivalent to auto [var1, var2] = classMember.funcThatReturnsATupple(num)?

auto var1 = classMember.funcThatReturnsATupple(num); 
auto var2 = classMember.funcThatReturnsATupple(num);

auto [var1, var2] calls a function which return a tuple, which has two elements(std::tuple<int, int>). Does the fist value of the tuple get stored in var1 and the second one in var2? I get the error that var1 and var2 are not declared. Do I need to declare them in doSmth()?

user438383
  • 5,716
  • 8
  • 28
  • 43
yad0
  • 77
  • 7
  • Its structure binding. See also [what is the purpose of the [ \] that follow after auto keyword?](https://stackoverflow.com/questions/72726812/what-is-the-purpose-of-the-that-follow-after-auto-keyword) – Jason Jun 26 '22 at 10:50

0 Answers0