Cannot capture static member with nested lambda

2019-07-15 14:07发布

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

1条回答
Viruses.
2楼-- · 2019-07-15 14:42

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

auto k = ::foo::_i + 1;
查看更多
登录 后发表回答