lambda expression problem - by Ken Domino

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.

Sign in
to vote
ID 746135 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 6/3/2012 5:33:32 AM
Access Restriction Public


'm using a lambda expression to specialize a class that I don't want to sub-class, but I'm having difficulty in compiling certain expressions, and the error messages are not useful. The code that reproduces the compilation problem is the following:

#include <iostream>
#include <functional>

using namespace std;

class M
    function<int (int r, int c)> fun;

        fun = [=] (int r, int c) -> int
            return r * 2 + c;

void foo(M * A)
    int test1 = A->fun(1,1);
    cout << test1 << "\n";

    auto f1 = [=]()
        int test = A->fun(2,2);
        cout << test << "\n";

    int * a = (int*)malloc(sizeof(int));
    auto f2 =
        [=] ()
            *a = 2;
    cout << *a << "\n";

    //auto f3 =
    //    [=] ()
    //    {
    //        int test = A->fun(3,3);
    //        cout << test << "\n";
    //        *a = test;
    //    };

int main()
    M * A = new M();
    return 0;
The code as it is shown compiles and works fine.  The problem occurs when I try to uncomment the block of code defining f3 and making the call to it.

This code illustrates the problem because in the definition of f1 and f2, those lambda expressions and calls work fine, but when combined, the expression does not compile.

I'm using Visual Studio 11 C++ Beta on Windows 7 x64. I haven't tried this yet on the just-released Release Preview.
Sign in to post a comment.
Posted by Microsoft on 9/6/2012 at 3:55 PM
Hi Ken:
    A fix for this issue has been checked into the compiler sources. The fix should show up in the next major release of Visual C++.
    To workaround the issue in VS2012 or earlier version, you can change the name of the captured variable.

Xiang Fan
Visual C++ Team
Posted by Microsoft on 6/7/2012 at 12:40 AM
Thank you for submitting feedback on Visual Studio 11 and .NET Framework. Your issue has been routed to the appropriate VS development team for review. We will contact you if we require any additional information.

Posted by MS-Moderator01 on 6/3/2012 at 5:42 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(