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

  • SEARCH
  • Home
  • 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 6746289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:18:18+00:00 2026-05-26T12:18:18+00:00

The following code compiles with gcc 4.5.1 but not with VS2010 SP1: #include <iostream>

  • 0

The following code compiles with gcc 4.5.1 but not with VS2010 SP1:

#include <iostream>
#include <vector>
#include <map>
#include <utility>
#include <set>
#include <algorithm>
 
using namespace std;
class puzzle
{
    vector<vector<int>> grid;
    map<int,set<int>> groups;
public:
    int member_function();
};
 
int puzzle::member_function()
{
    int i;
    for_each(groups.cbegin(), groups.cend(), [grid, &i](pair<int,set<int>> group) {
        i++;
        cout << i << endl;
    });
}

This is the error:

error C3480: 'puzzle::grid': a lambda capture variable must be from an enclosing function scope
warning C4573: the usage of 'puzzle::grid' requires the compiler to capture 'this' but the current default capture mode does not allow it
  1. Which compiler is right?
  2. How can I use data members inside a lambda in VS2010?
  • 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-26T12:18:18+00:00Added an answer on May 26, 2026 at 12:18 pm

    I believe VS2010 to be right this time, and I’d check if I had the standard handy, but currently I don’t.

    Now, it’s exactly like the error message says: You can’t capture stuff outside of the enclosing scope of the lambda.† grid is not in the enclosing scope, but this is (every access to grid actually happens as this->grid in member functions). For your usecase, capturing this works, since you’ll use it right away and you don’t want to copy the grid

    auto lambda = [this](){ std::cout << grid[0][0] << "\n"; }
    

    If however, you want to store the grid and copy it for later access, where your puzzle object might already be destroyed, you’ll need to make an intermediate, local copy:

    vector<vector<int> > tmp(grid);
    auto lambda = [tmp](){}; // capture the local copy per copy
    

    † I’m simplifying – Google for “reaching scope” or see §5.1.2 for all the gory details.

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

Sidebar

Related Questions

The following code compiles in MSVC++, but does not compile in GCC 4.5.1: #include
The following code does not compile with gcc 4.7 (20120114) , but compiles fine
The following code compiles in Visual C++ and gcc, but fails with Code Warrior
I have the following code compiled by gcc: #include <iostream> using namespace std; class
I am unclear about the following. First, this code compiles fine: #include <vector> typedef
Following code does compile in gcc 4.5 but it is not being compiled in
The following code compiles and runs fine with gcc. But i wondered if such
The following code goes into an infinite loop on GCC: #include <iostream> using namespace
The following code works with GCC's C compiler, but not with the C++ compiler.
The following code doesn't compile with gcc, but does with Visual Studio: template <typename

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.