1

Could someone explain what is happening here (GCC 7.3):

#include <thread>
#include <iostream>

struct A
{
    struct B {};
};

int main()
{
    int var = 0;
    std::thread([c=A::B(), var](){ });     // error: β€˜var’ was not declared in this scope
    std::thread([c=A(), var](){ });        // OK
    std::thread([c=A::B(), var=var](){ }); // OK
    return 0;
}

When I capture nested struct I got:

'var' was not declared in this scope

On the other hand, capturing non-nested struct works. Also capturing with initialization works. Also all cases work in Visual Studio.

lstipakov
  • 3,138
  • 5
  • 31
  • 46
  • 1
    Use a recent compiler... – user1810087 Feb 07 '20 at 09:36
  • 2
    [Here](https://godbolt.org/) you can test many G++ versions. Works starting at `g++8.1`. If you want to stay with g++7.x just reverse order: `[var, c=A::B()]` - [working](https://godbolt.org/z/T7fos5). – rafix07 Feb 07 '20 at 09:39

1 Answers1

2

Must be a bug in GCC 7.x since 8.1 accepts it. https://godbolt.org/z/xXw6qN

Surt
  • 15,501
  • 3
  • 23
  • 39