Search

Unexpected behaviour of this capture in a lambda expression by Matthew Bradbury

Closed
as Fixed Help for as Fixed

2
0
Sign in
to vote
Type: Bug
ID: 713679
Opened: 12/13/2011 5:51:34 AM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
0
Workaround(s)
1
User(s) can reproduce this bug
Extra qualification of class members is needed in a lambda expression capturing this, when the member name is the same as a global function.
Details (expand)

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

Visual Studio 11 Developer Preview

Steps to reproduce

#include <ctime>
#include <string>

void call(std::string p) {}

struct C
{
    std::string time;

    void bad()
    {
        [this]() { call(time); };
    }

    void good()
    {
        [this]() { call(this->time); };
    }
};

Product Language

English

Operating System

Windows 7

Operating System Language

English

Actual results

error C2664: 'call' : cannot convert parameter 1 from 'time_t (__cdecl *)(time_t *)' to 'std::string'
No constructor could take the source type, or constructor overload resolution was ambiguous

Expected results

Expected it to compile fine without the extra this qualifier
File Attachments
0 attachments
Sign in to post a comment.
Posted by Jim Barry on 2/8/2012 at 5:00 AM
I just ran into this bug myself, in VC10. It's particularly nasty when a member function has the same name and parameters as an existing nonmember function. The program compiles, but the nonmember function is called instead of the member function. I think this is quite bad.

Example:

#include <iostream>

void f()
{
std::cout << "Called ::f()" << std::endl;
}

struct S
{
void f()
{
    std::cout << "Called S::f()" << std::endl;
}

void oops()
{
    [this](){ f(); }(); // calls the wrong function
}
};

int main()
{
S().oops();
}
Posted by MS-Moderator09 [Feedback Moderator] on 12/13/2011 at 11:45 PM
Thank you for submitting feedback on Visual Studio 2010 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 12/13/2011 at 6: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.