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

    1. <legend id='j060e'><style id='j060e'><dir id='j060e'><q id='j060e'></q></dir></style></legend>

        <bdo id='j060e'></bdo><ul id='j060e'></ul>

    2. <tfoot id='j060e'></tfoot>

    3. <small id='j060e'></small><noframes id='j060e'>

        lambdas 需要捕获“this"来调用静态成员函数?

        时间:2023-07-01
        • <small id='Z1Uoc'></small><noframes id='Z1Uoc'>

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

          <tfoot id='Z1Uoc'></tfoot>

            • <bdo id='Z1Uoc'></bdo><ul id='Z1Uoc'></ul>

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

                1. 本文介绍了lambdas 需要捕获“this"来调用静态成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  对于以下代码:

                  struct B
                  {
                      void g()
                      {
                          []() { B::f(); }();
                      }
                  
                      static void f();
                  };
                  

                  g++ 4.6 给出错误:

                  g++ 4.6 gives the error:

                  test.cpp:在 lambda 函数中:
                  test.cpp:44:21: 错误:此 lambda 函数未捕获this"

                  test.cpp: In lambda function:
                  test.cpp:44:21: error: 'this' was not captured for this lambda function

                  (有趣的是,g++ 4.5 编译代码很好).

                  (Interestingly, g++ 4.5 compiles the code fine).

                  这是 g++ 4.6 中的错误,还是真的有必要捕获this"参数才能调用静态成员函数?我不明白为什么会这样,我什至用 B:: 限定了调用.

                  Is this a bug in g++ 4.6, or is it really necessary to capture the 'this' parameter to be able to call a static member function? I don't see why it should be, I even qualified the call with B::.

                  推荐答案

                  我同意,它应该编译得很好.对于修复(如果您还不知道),只需添加引用捕获,它就会在 gcc 4.6 上正常编译

                  I agree, it should compile just fine. For the fix (if you didn't know already), just add the reference capture and it will compile fine on gcc 4.6

                  struct B
                  {
                      void g()
                      {
                          [&]() { B::f(); }();
                      }
                  
                      static void f() { std::cout << "Hello World" << std::endl; };
                  };
                  

                  这篇关于lambdas 需要捕获“this"来调用静态成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 lambdas 进行变量访问的最佳方法 下一篇:对于 lambda 来说,本质上来说,它自己关闭是否有效?

                  相关文章

                    <tfoot id='zyMmw'></tfoot>

                    <small id='zyMmw'></small><noframes id='zyMmw'>

                    1. <legend id='zyMmw'><style id='zyMmw'><dir id='zyMmw'><q id='zyMmw'></q></dir></style></legend>

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