Search

lambda expression problem by Ken Domino

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)
0
User(s) can reproduce this bug
'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 (expand)

Visual Studio/Team Foundation Server/.NET Framework Tooling Version

Visual Studio 2012 RC

Steps to reproduce

Compile the code without the comments. To work around the problem, change the name of the variable "a" to "x". That fixes the problem. It isn't clear why "a" causes the problem.

Product Language

English

Operating System

Windows 7

Operating System Language

English

Actual results


1>ClCompile:
1> Source.cpp
1>c:\users\ken\documents\visual studio 11\projects\consoleapplication8\consoleapplication8\source.cpp(48): error C2371: '_A' : redefinition; different basic types
1>         c:\users\ken\documents\visual studio 11\projects\consoleapplication8\consoleapplication8\source.cpp(48) : see declaration of '_A'
1>c:\users\ken\documents\visual studio 11\projects\consoleapplication8\consoleapplication8\source.cpp(48): error C2661: 'foo::<lambda_4b1774fd83e66fa4519f32071f6f1dd4>::<lambda_4b1774fd83e66fa4519f32071f6f1dd4>' : no overloaded function takes 2 arguments
1>c:\users\ken\documents\visual studio 11\projects\consoleapplication8\consoleapplication8\source.cpp(49): error C3536: 'f3': cannot be used before it is initialized
1>c:\users\ken\documents\visual studio 11\projects\consoleapplication8\consoleapplication8\source.cpp(49): error C2064: term does not evaluate to a function taking 0 arguments
1>c:\users\ken\documents\visual studio 11\projects\consoleapplication8\consoleapplication8\source.cpp(48): error C2440: 'initializing' : cannot convert from 'M *const ' to 'int *'
1>         Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\ken\documents\visual studio 11\projects\consoleapplication8\consoleapplication8\source.cpp(48): error C2439: 'foo::<lambda_4b1774fd83e66fa4519f32071f6f1dd4>::a' : member could not be initialized
1>         c:\users\ken\documents\visual studio 11\projects\consoleapplication8\consoleapplication8\source.cpp(48) : see declaration of 'foo::<lambda_4b1774fd83e66fa4519f32071f6f1dd4>::a'

Expected results

No compiler errors, and compiled program should run.
File Attachments
0 attachments
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.