C++ Lambda capture private class member

后端 未结 1 1526
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-12 13:47

Here is a part of my Class:

//...
        bool dump_dvars()
        {
            fstream& refvar{ output_file };

            for_each(_array_start, _array_         


        
相关标签:
1条回答
  • 2021-02-12 14:38

    You should capture 'this' in the lambda.

    The following code compiles and works just fine with g++, clang, VS2010 and VS2013.

    #include <iostream>
    
    class A
    {
    public:
        A() : x(5){}
        void f() const
        {
            ([this]()
            {
                std::cout << x << std::endl;
            })();
        }
    private:
        int x;
    };
    
    int main()
    {
        A a;
        a.f();
    }
    
    0 讨论(0)
提交回复
热议问题