1. <tfoot id='jtgd2'></tfoot>
    1. <i id='jtgd2'><tr id='jtgd2'><dt id='jtgd2'><q id='jtgd2'><span id='jtgd2'><b id='jtgd2'><form id='jtgd2'><ins id='jtgd2'></ins><ul id='jtgd2'></ul><sub id='jtgd2'></sub></form><legend id='jtgd2'></legend><bdo id='jtgd2'><pre id='jtgd2'><center id='jtgd2'></center></pre></bdo></b><th id='jtgd2'></th></span></q></dt></tr></i><div id='jtgd2'><tfoot id='jtgd2'></tfoot><dl id='jtgd2'><fieldset id='jtgd2'></fieldset></dl></div>

        <bdo id='jtgd2'></bdo><ul id='jtgd2'></ul>
    2. <small id='jtgd2'></small><noframes id='jtgd2'>

      <legend id='jtgd2'><style id='jtgd2'><dir id='jtgd2'><q id='jtgd2'></q></dir></style></legend>

    3. 在成员函数内的 lambda 捕获列表中使用成员变量

      时间:2023-05-23
            • <small id='GV09Q'></small><noframes id='GV09Q'>

                <bdo id='GV09Q'></bdo><ul id='GV09Q'></ul>
                <tfoot id='GV09Q'></tfoot>

                <legend id='GV09Q'><style id='GV09Q'><dir id='GV09Q'><q id='GV09Q'></q></dir></style></legend>

                  <tbody id='GV09Q'></tbody>
              • <i id='GV09Q'><tr id='GV09Q'><dt id='GV09Q'><q id='GV09Q'><span id='GV09Q'><b id='GV09Q'><form id='GV09Q'><ins id='GV09Q'></ins><ul id='GV09Q'></ul><sub id='GV09Q'></sub></form><legend id='GV09Q'></legend><bdo id='GV09Q'><pre id='GV09Q'><center id='GV09Q'></center></pre></bdo></b><th id='GV09Q'></th></span></q></dt></tr></i><div id='GV09Q'><tfoot id='GV09Q'></tfoot><dl id='GV09Q'><fieldset id='GV09Q'></fieldset></dl></div>
                本文介绍了在成员函数内的 lambda 捕获列表中使用成员变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                以下代码使用 gcc 4.5.1 编译但不适用于 VS2010 SP1:

                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;
                        });
                }
                int main()
                {
                        return 0;
                }
                

                这是错误:

                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> 哪个编译器是对的?

                1> which compiler is right?

                2> 如何在 VS2010 的 lambda 中使用成员变量?

                2> How can I use member variables inside a lambda in VS2010?

                推荐答案

                我相信 VS2010 这次是正确的,我会检查我是否有方便的标准,但目前我没有.

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

                现在,就像错误消息所说的那样:您无法捕获 lambda 封闭范围之外的内容. grid 不在封闭范围内,但是 this 是(在成员函数中,对 grid 的每次访问实际上都是作为 this->grid 发生的).对于您的用例,捕获 this 有效,因为您将立即使用它并且您不想复制 grid

                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] << "
                "; }
                

                但是,如果您想要存储网格并复制它以供以后访问,而您的 puzzle 对象可能已经被销毁,则您需要制作一个中间的本地副本:

                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
                

                <小时>

                † 我正在简化 - 谷歌搜索达到范围"或参阅第 5.1.2 节了解所有血腥细节.


                † I'm simplifying - Google for "reaching scope" or see §5.1.2 for all the gory details.

                这篇关于在成员函数内的 lambda 捕获列表中使用成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:从不同 VS2010 项目中的 C++ 代码调用 C 函数时出现 下一篇:Visual Studio 2010 的奇怪“警告 LNK4042"

                相关文章

                最新文章

                1. <i id='rvsaL'><tr id='rvsaL'><dt id='rvsaL'><q id='rvsaL'><span id='rvsaL'><b id='rvsaL'><form id='rvsaL'><ins id='rvsaL'></ins><ul id='rvsaL'></ul><sub id='rvsaL'></sub></form><legend id='rvsaL'></legend><bdo id='rvsaL'><pre id='rvsaL'><center id='rvsaL'></center></pre></bdo></b><th id='rvsaL'></th></span></q></dt></tr></i><div id='rvsaL'><tfoot id='rvsaL'></tfoot><dl id='rvsaL'><fieldset id='rvsaL'></fieldset></dl></div>

                  <legend id='rvsaL'><style id='rvsaL'><dir id='rvsaL'><q id='rvsaL'></q></dir></style></legend>

                  <tfoot id='rvsaL'></tfoot>
                      <bdo id='rvsaL'></bdo><ul id='rvsaL'></ul>
                  1. <small id='rvsaL'></small><noframes id='rvsaL'>