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

      <tfoot id='JTflD'></tfoot>

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

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

      1. 为什么我不能在 C++11 中创建一个 lambda 向量(相同

        时间:2023-09-15
          <bdo id='jpgTc'></bdo><ul id='jpgTc'></ul>

              <legend id='jpgTc'><style id='jpgTc'><dir id='jpgTc'><q id='jpgTc'></q></dir></style></legend>
            1. <small id='jpgTc'></small><noframes id='jpgTc'>

                  <tbody id='jpgTc'></tbody>

                <tfoot id='jpgTc'></tfoot>

              • <i id='jpgTc'><tr id='jpgTc'><dt id='jpgTc'><q id='jpgTc'><span id='jpgTc'><b id='jpgTc'><form id='jpgTc'><ins id='jpgTc'></ins><ul id='jpgTc'></ul><sub id='jpgTc'></sub></form><legend id='jpgTc'></legend><bdo id='jpgTc'><pre id='jpgTc'><center id='jpgTc'></center></pre></bdo></b><th id='jpgTc'></th></span></q></dt></tr></i><div id='jpgTc'><tfoot id='jpgTc'></tfoot><dl id='jpgTc'><fieldset id='jpgTc'></fieldset></dl></div>
                1. 本文介绍了为什么我不能在 C++11 中创建一个 lambda 向量(相同类型)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我试图创建一个 lambda 向量,但失败了:

                  I was trying to create a vector of lambda, but failed:

                  auto ignore = [&]() { return 10; };  //1
                  std::vector<decltype(ignore)> v;     //2
                  v.push_back([&]() { return 100; });  //3
                  

                  直到第 2 行,它编译良好.但是第 3 行给出了编译错误:

                  Up to line #2, it compiles fine. But the line#3 gives compilation error:

                  错误:没有匹配的函数调用'std::vector<main()::<lambda()>>::push_back(main()::<lambda()>)'

                  error: no matching function for call to 'std::vector<main()::<lambda()>>::push_back(main()::<lambda()>)'

                  我不想要函数指针向量或函数对象向量.然而,封装real lambda 表达式的函数对象向量对我有用.这可能吗?

                  I don't want a vector of function pointers or vector of function objects. However, vector of function objects which encapsulate real lambda expressions, would work for me. Is this possible?

                  推荐答案

                  每个 lambda 都有不同的类型—即使它们具有相同的签名.如果你想做这样的事情,你必须使用一个运行时封装容器,比如 std::function.

                  Every lambda has a different type—even if they have the same signature. You must use a run-time encapsulating container such as std::function if you want to do something like that.

                  例如:

                  std::vector<std::function<int()>> functors;
                  functors.push_back([&] { return 100; });
                  functors.push_back([&] { return  10; });
                  

                  这篇关于为什么我不能在 C++11 中创建一个 lambda 向量(相同类型)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++ 编译时错误:数字常量之前的预期标识符 下一篇:为什么 emplace_back() 不使用统一初始化?

                  相关文章

                  最新文章

                2. <tfoot id='nQwmH'></tfoot><legend id='nQwmH'><style id='nQwmH'><dir id='nQwmH'><q id='nQwmH'></q></dir></style></legend>

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

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

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