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

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

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

        • <bdo id='w0679'></bdo><ul id='w0679'></ul>
      1. C++ 中的重载 lambda 以及 clang 和 gcc 之间的差异

        时间:2023-09-18
          <bdo id='ZJsXm'></bdo><ul id='ZJsXm'></ul>

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

            <tbody id='ZJsXm'></tbody>

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

              <i id='ZJsXm'><tr id='ZJsXm'><dt id='ZJsXm'><q id='ZJsXm'><span id='ZJsXm'><b id='ZJsXm'><form id='ZJsXm'><ins id='ZJsXm'></ins><ul id='ZJsXm'></ul><sub id='ZJsXm'></sub></form><legend id='ZJsXm'></legend><bdo id='ZJsXm'><pre id='ZJsXm'><center id='ZJsXm'></center></pre></bdo></b><th id='ZJsXm'></th></span></q></dt></tr></i><div id='ZJsXm'><tfoot id='ZJsXm'></tfoot><dl id='ZJsXm'><fieldset id='ZJsXm'></fieldset></dl></div>
              • <tfoot id='ZJsXm'></tfoot>
                1. 本文介绍了C++ 中的重载 lambda 以及 clang 和 gcc 之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在玩一个在 C++ 中重载 lambdas 的技巧.具体:

                  I'm playing with a trick to overload lambdas in C++. Specifically:

                  // For std::function
                  #include <functional>
                  
                  // For std::string
                  #include <string>
                  
                  // For std::cout
                  #include <iostream>
                  
                  template <class... F>
                  struct overload : F... {
                      overload(F... f) : F(f)... {}
                  };      
                  
                  template <class... F>
                  auto make_overload(F... f) {
                      return overload<F...>(f...);
                  }
                  
                  int main() {
                  
                      std::function <int(int,int)> f = [](int x,int y) {
                          return x+y;
                      };
                      std::function <double(double,double)> g = [](double x,double y) {
                          return x+y;
                      };
                      std::function <std::string(std::string,std::string)> h = [](std::string x,std::string y) {
                          return x+y;
                      };
                  
                      auto fgh = make_overload(f,g,h);
                      std::cout << fgh(1,2) << std::endl;
                      std::cout << fgh(1.5,2.5) << std::endl;
                      std::cout << fgh("bob","larry") << std::endl;
                  }
                  

                  现在,上面的程序在clang中编译并运行良好:

                  Now, the above program compiles and works fine in clang:

                  $ clang++ -g -std=c++14 test01.cpp -o test01
                  $ ./test01
                  3
                  4
                  boblarry
                  

                  它不会在 gcc 中编译:

                  It does not compile in gcc:

                  $ g++ -g -std=c++14 test01.cpp -o test01
                  test01.cpp: In function 'int main()':
                  test01.cpp:36:25: error: request for member 'operator()' is ambiguous
                       std::cout << fgh(1,2) << std::endl;
                                           ^
                  In file included from test01.cpp:5:0:
                  /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.2/include/g++-v4/functional:2434:5: note: candidates are: _Res std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
                       function<_Res(_ArgTypes...)>::
                       ^
                  /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.2/include/g++-v4/functional:2434:5: note:                 _Res std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = double; _ArgTypes = {double, double}]
                  /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.2/include/g++-v4/functional:2434:5: note:                 _Res std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = int; _ArgTypes = {int, int}]
                  test01.cpp:37:29: error: request for member 'operator()' is ambiguous
                       std::cout << fgh(1.5,2.5) << std::endl;
                                               ^
                  In file included from test01.cpp:5:0:
                  /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.2/include/g++-v4/functional:2434:5: note: candidates are: _Res std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
                       function<_Res(_ArgTypes...)>::
                       ^
                  /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.2/include/g++-v4/functional:2434:5: note:                 _Res std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = double; _ArgTypes = {double, double}]
                  /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.2/include/g++-v4/functional:2434:5: note:                 _Res std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = int; _ArgTypes = {int, int}]
                  test01.cpp:38:35: error: request for member 'operator()' is ambiguous
                       std::cout << fgh("bob","larry") << std::endl;
                                                     ^
                  In file included from test01.cpp:5:0:
                  /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.2/include/g++-v4/functional:2434:5: note: candidates are: _Res std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
                       function<_Res(_ArgTypes...)>::
                       ^
                  /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.2/include/g++-v4/functional:2434:5: note:                 _Res std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = double; _ArgTypes = {double, double}]
                  /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.2/include/g++-v4/functional:2434:5: note:                 _Res std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = int; _ArgTypes = {int, int}]
                  Makefile:2: recipe for target 'all' failed
                  make: *** [all] Error 1
                  

                  为什么会有不同?作为记录,我使用的是 gcc 4.9.2 和 clang 3.5.0.

                  Why is there a difference? For the record, I'm using gcc 4.9.2 and clang 3.5.0.

                  显然,这段代码也无法在 VC 上编译,并且已经报告.话虽如此,Sean Middleditch 发布了重载代码的工作版本:

                  Evidently, this snippet of code failed to compile on VC as well and had already been reported. That being said, Sean Middleditch posted a working version of the overloaded code:

                  template<class F1, class... Fs>
                  struct overload : F1, overload<Fs...>
                  {
                      using F1::operator();
                      using overload<Fs...>::operator();
                      overload(F1 f1, Fs... fs) : F1(f1), overload<Fs...>(fs...) {}
                  };
                  
                  template<class F1>
                  struct overload<F1> : F1
                  {
                      using F1::operator();
                      overload(F1 f1) : F1(f1) {}
                  };
                  
                  
                  template <class... F>
                  auto make_overload(F... f) {
                      return overload<F...>(f...);
                  }
                  

                  我仍然有兴趣了解为什么这个版本的重载 lambda 代码可以工作,但原始版本不行.

                  I'm still interested in understanding why this version of the overloaded lambda code works, but the original one does not.

                  推荐答案

                  在我看来像是 Clang 错误.

                  Looks like a Clang bug to me.

                  一般规则是不同基类中同名的成员函数不重载.例如:

                  The general rule is that member functions of the same name in different base classes do not overload. For example:

                  struct Foo { void bar(); };
                  struct Baz { void bar(int); };
                  struct Quux : Foo, Baz { };
                  
                  int main() { Quux().bar(); } // error on both GCC and Clang
                  

                  无论出于何种原因,Clang 未能诊断 operator() 的这种歧义.

                  For whatever reason, Clang fails to diagnose this ambiguity for operator().

                  using-declaration 将命名的基类成员提升到派生类范围,允许它们重载.因此:

                  A using-declaration lifts the named base class members to the derived class scope, allowing them to overload. Hence:

                  struct Quux_2 : Foo, Baz { using Foo::bar; using Baz::bar; };
                  Quux_2().bar(); // OK.
                  

                  在代码的工作版本中,using 声明递归地将模板参数中的每个 operator() 声明带入最派生类的范围,允许它们超载.

                  In the working version of the code, the using declarations recursively bring every operator() declaration in the template arguments into the scope of the most derived class, allowing them to overload.

                  这篇关于C++ 中的重载 lambda 以及 clang 和 gcc 之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在基准测试时防止编译器优化 下一篇:如何在 GCC 中使用 OpenSSL?

                  相关文章

                  最新文章

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

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