<bdo id='7fKMP'></bdo><ul id='7fKMP'></ul>
  1. <legend id='7fKMP'><style id='7fKMP'><dir id='7fKMP'><q id='7fKMP'></q></dir></style></legend>

  2. <small id='7fKMP'></small><noframes id='7fKMP'>

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

      Lambda 函数作为基类

      Lambda functions as base classes(Lambda 函数作为基类)
      <i id='FHxXT'><tr id='FHxXT'><dt id='FHxXT'><q id='FHxXT'><span id='FHxXT'><b id='FHxXT'><form id='FHxXT'><ins id='FHxXT'></ins><ul id='FHxXT'></ul><sub id='FHxXT'></sub></form><legend id='FHxXT'></legend><bdo id='FHxXT'><pre id='FHxXT'><center id='FHxXT'></center></pre></bdo></b><th id='FHxXT'></th></span></q></dt></tr></i><div id='FHxXT'><tfoot id='FHxXT'></tfoot><dl id='FHxXT'><fieldset id='FHxXT'></fieldset></dl></div>

        <tbody id='FHxXT'></tbody>
      • <legend id='FHxXT'><style id='FHxXT'><dir id='FHxXT'><q id='FHxXT'></q></dir></style></legend>

        <tfoot id='FHxXT'></tfoot>
        • <bdo id='FHxXT'></bdo><ul id='FHxXT'></ul>

              • <small id='FHxXT'></small><noframes id='FHxXT'>

                本文介绍了Lambda 函数作为基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在使用 Lambda 时,我发现了一个我不完全理解的有趣行为.

                Playing around with Lambdas I found an interesting behaviour that I do not fully understand.

                假设我有一个从 2 个模板参数派生的 struct Overload,并且有一个 using F1::operator(); 子句.

                Supose I have a struct Overload that derives from 2 template parameters, and has a using F1::operator(); clause.

                现在如果我从两个函子派生,我只能访问 F1 的 operator()(正如我所期望的)

                Now if I derive from two functors I can only access the operator() of F1 (as I would expect)

                如果我从两个 Lambda 函数派生,这将不再正确:我也可以从 F2 访问 operator().

                If I derive from two Lambda Functions this is no longer true: I can access the operator() from F2 too.

                #include <iostream>
                
                // I compiled with g++ (GCC) 4.7.2 20121109 (Red Hat 4.7.2-8)
                //
                // g++ -Wall -std=c++11 -g main.cc
                // g++ -Wall -std=c++11 -DFUNCTOR -g main.cc
                // 
                // or clang clang version 3.3 (tags/RELEASE_33/rc2)
                // 
                // clang++ -Wall -std=c++11 -g main.cc
                // clang++ -Wall -std=c++11 -DFUNCTOR -g main.cc
                // 
                // on a Linux localhost.localdomain 3.9.6-200.fc18.i686 #1 SMP Thu Jun 13 
                // 19:29:40 UTC 2013 i686 i686 i386 GNU/Linux box
                
                
                struct Functor1
                {
                    void operator()() { std::cout << "Functor1::operator()()
                "; }
                };
                
                struct Functor2
                {
                    void operator()(int) { std::cout << "Functor2::operator()(int)
                "; }
                };
                
                template <typename F1, typename F2>
                struct Overload : public F1, public F2
                {
                    Overload()
                        : F1()
                        , F2() {}
                
                    Overload(F1 x1, F2 x2)
                        : F1(x1)
                        , F2(x2) {}
                
                    using F1::operator(); 
                };
                
                template <typename F1, typename F2>
                auto get(F1 x1, F2 x2) -> Overload<F1, F2>
                {
                   return Overload<F1, F2>(x1, x2);
                }
                
                
                int main(int argc, char *argv[])
                {
                    auto f = get(Functor1(), Functor2());
                
                    f();
                #ifdef FUNCTOR
                    f(2); // this one doesn't work IMHO correctly
                #endif
                
                    auto f1 = get(
                                  []() { std::cout << "lambda1::operator()()
                "; },
                                  [](int) { std::cout << "lambda2::operator()(int)
                "; }
                                  );
                    f1();
                    f1(2); // this one works but I don't know why
                
                
                  return 0;
                }
                

                标准规定:

                lambda 表达式的类型(也是闭包对象的类型)是唯一的、未命名的非联合类类型

                The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed non- union class type

                所以每个 Lambda 的类型都应该是唯一的.

                So every Lambda's types should be unique.

                我无法解释为什么会这样:有人能解释一下吗?

                I cannot explain why this is so: can anyone shed some light on this please?

                推荐答案

                除了 operator() 之外,由 lambda 定义的类可以(在适当的情况下)提供到指向函数的指针.情况(或至少是主要情况)是 lambda 无法捕获任何内容.

                In addition to operator(), a the class defined by a lambda can (under the right circumstances) provide a conversion to a pointer to function. The circumstance (or at least the primary one) is that the lambda can't capture anything.

                如果您添加捕获:

                auto f1 = get(
                              []() { std::cout << "lambda1::operator()()
                "; },
                              [i](int) { std::cout << "lambda2::operator()(int)
                "; }
                              );
                f1();
                f1(2);
                

                ...不再提供到 pointer to function 的转换,因此尝试编译上面的代码会给出您可能一直期望的错误:

                ...the conversion to pointer to function is no longer provided, so trying to compile the code above gives the error you probably expected all along:

                trash9.cpp: In function 'int main(int, char**)':
                trash9.cpp:49:9: error: no match for call to '(Overload<main(int, char**)::<lambda()>, main(int, char**)::<lambda(int)> >) (int)'
                trash9.cpp:14:8: note: candidate is:
                trash9.cpp:45:23: note: main(int, char**)::<lambda()>
                trash9.cpp:45:23: note:   candidate expects 0 arguments, 1 provided
                

                这篇关于Lambda 函数作为基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to print vector#39;s data(如何打印矢量的数据)
                Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                MSVC++ warning flags(MSVC++ 警告标志)
                How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                stack overflow error in C++ program(C++程序中的堆栈溢出错误)
                  <bdo id='Cfl6U'></bdo><ul id='Cfl6U'></ul>

                        <legend id='Cfl6U'><style id='Cfl6U'><dir id='Cfl6U'><q id='Cfl6U'></q></dir></style></legend>
                        <tfoot id='Cfl6U'></tfoot>

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

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