Understanding C++0x lambda captures

前端 未结 1 2033
情书的邮戳
情书的邮戳 2021-02-13 18:55

In one of the recent C++0x drafts (n3225.pdf) we can find 5.1.2/10:

The identifiers in a capture-list are looked up using the usual rules for unqualified

相关标签:
1条回答
  • 2021-02-13 19:37

    From what you've posted it seems like your first example is illegal since neither captured variable has automatic duration. However, you can easily fix this. To capture the data member, you can just capture this, and the global doesn't need to be captured as you can just reference it directly.

    EDIT: As you pointed out, this will not create a local copy of the value you want to capture. To capture these variables while making a copy, you can capture this, then explicitly create a local copy of the data member inside of the lambda.

    As for the second question about capturing references, §5.1.2/14 says that capturing a variable of reference type by copy will create a copy of the value referenced instead of creating a copy of the reference. Thus the lambda will have its own copy of the value that the reference was referencing when it was created.

    0 讨论(0)
提交回复
热议问题