Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6710823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:04:01+00:00 2026-05-26T08:04:01+00:00

Consider the following code: class A { public: void foo() { auto functor =

  • 0

Consider the following code:

class A
{
public:
    void foo()
    {
        auto functor = [this]() 
            {
                A * a = this;
                auto functor = [a]() // The compiler won't accept "this" instead of "a"
                    {
                        a->bar();
                    };
            };
    }

    void bar() {}
};

In VC2010, using this instead of a lead to compilation errors. Among others:

1>main.cpp(20): error C3480: '`anonymous-namespace'::<lambda0>::__this': a lambda capture variable must be from an enclosing function scope
1>main.cpp(22): error C3493: 'this' cannot be implicitly captured because no default capture mode has been specified

Which I don’t understand. Does it mean it doesn’t know if it should use a reference or copy it? When trying to use &this to force referencing, it also says:

1>main.cpp(20): error C3496: 'this' is always captured by value: '&' ignored

The temporary is not that annoying, but for the sake of curiosity, is there a way to get rid of it? What goes on when this is given to a lambda?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:04:02+00:00Added an answer on May 26, 2026 at 8:04 am

    This appears to be a compiler bug in VS2010. I was able to make it work by letting the inner lambda implicitly capture this:

    class A
    {
    public:
        void foo()
        {
            auto functor = [this]() 
            {
                auto functor = [=]()
                {
                    bar();
                };
            };
        }
    
        void bar() {}
    };
    

    When trying to use &this to force referencing, it also says:

    1>main.cpp(20): error C3496: ‘this’ is always captured by value: ‘&’ ignored

    this can only be captured by value. [=] and [&] both capture it by value.

    What goes on when this is given to a lambda?

    I don’t know but it must be something special because you can’t use this in a lambda as a pointer to the lambda object. Anything else captured becomes a private member of the lambda so presumably this does too but there’s some special handling on usage.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: public class Bar { Foo foo; void Go() { foo
Consider the following code: class A { public: virtual void f() throw ( int
Consider the following C++ code: class A { public: virtual void f()=0; }; int
Consider the following short code snippet. namespace B { public class Foo { public
Consider the following code: public class Foo { private static final Object LOCK =
Consider the following code: public class Vehicle { public void StartEngine() { // Code
Consider the following code: abstract class Foo<T> where T : Foo<T>, new() { void
Consider the following sample code: class MyClass { public long x; public void DoWork()
Consider the following C# code: class Program { static public void Print(string toPrint) {
Consider the following code:- class Name { {System.out.println(hi);} public static void main(String[] args) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.