2

I have a problem with a nested lambda function which cannot see a static class member. Visual Studio 2010 gives me a C2065 ( undeclared identifier ) for reasons I cannot understand.

Here is simple case that highlights my problem:

#include <algorithm>
#include <vector>

using namespace std;

struct foo
{
    void do_some()
    {
        std::vector<int> a;
        std::vector<int> b;

        for_each( a.begin(), a.end(), [&] ( const int& m )
            {
                // works
                auto j = _i + 1;

                for_each( b.begin(), b.end(), [&] ( const int& n )
                    {
                        **// doesn't work**
                        auto k = _i + 1;
                    } );
            } );
    }

    static int _i;
};

int main(int argc, char* argv[])
{
}

Anyone knows what I'm doing wrong?

Thanks, Christian

3
  • 2
    This would not be a capture, it would be an ordinary odr-use. Looks valid, though. Does it help if you put foo::_i? Commented Jan 18, 2013 at 15:27
  • Yes that helps! Thanks. what's odr-use? Commented Jan 18, 2013 at 17:11
  • 1
    "odr-use" is a technical term from the C++ Standard roughly meaning a use of a declaration which requires a definition to exist, and is related to the One Definition Rule. Only function-local variables need to be captured for a lambda. Commented Jan 18, 2013 at 18:31

1 Answer 1

6

Probably a compiler bug (fixed in VC++ 2012). This works:

auto k = ::foo::_i + 1;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.