Home Dashboard Directory Help
Search

lambda expression problem by Ken Domino


Status: 

Closed
 as Fixed Help for as Fixed


1
0
Sign in
to vote
Type: Bug
ID: 746135
Opened: 6/3/2012 5:33:32 AM
Access Restriction: Public
0
Workaround(s)
view
0
User(s) can reproduce this bug

Description

'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
{
public:
    function<int (int r, int c)> fun;

    M()
    {
        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";
    };
    f1();

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

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


int main()
{
    M * A = new M();
    foo(A);
    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.
Details
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(http://support.microsoft.com)
Sign in to post a workaround.