Reference capture by inner lambda of by-value captured variable of non-mutable outer lambda causes weird compiler error - by c9xeo

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


2
0
Sign in
to vote
ID 730244 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 3/10/2012 9:23:41 AM
Access Restriction Public
Moderator Decision Sent to Engineering Team for consideration

Description

This code 

int main()
{
    int i = 0;
    [i]{
      [&]{ int j = i; };
    };
}

triggers the following error:

error C2440: '<function-style-cast>' : cannot convert from 'const int' to '`anonymous-namespace'::<lambda1>'
          No constructor could take the source type, or constructor overload resolution was ambiguous

The following changes make the code compile as expected:
- mark the outer lambda 'mutable'
- capture 'i' by reference in the outer lambda
- remove the 'int j = i;' in the inner lambda
- capture everything by value in the inner lambda
Presumably just capturing 'i' by value in the inner lambda would work too, but I can't test that because of https://connect.microsoft.com/VisualStudio/feedback/details/725134 not being fixed in VS11 Beta. This bug may be related to the aforementioned one, as inner lambdas seem to search the outer function block scope, and not the lambda block scope for captures.
Sign in to post a comment.
Posted by Microsoft on 3/18/2012 at 3:07 PM
Hello,

Thank you for your bug report. This is already fixed in the next version of the VC++ compiler, you may try it out using currently available VS11 Beta.

Thanks,
Ulzii Luvsanbat
Visual C++ Team
Posted by MS-Moderator09 [Feedback Moderator] on 3/12/2012 at 1:24 AM
Thanks for your feedback. We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.Thank you !
Posted by MS-Moderator01 on 3/10/2012 at 9:53 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)