<tfoot id='0Oybi'></tfoot>

      • <bdo id='0Oybi'></bdo><ul id='0Oybi'></ul>

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

      1. <small id='0Oybi'></small><noframes id='0Oybi'>

        消除作为模板参数传递的重载成员函数指针的歧

        时间:2023-05-25
      2. <small id='4e8VH'></small><noframes id='4e8VH'>

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

              <tfoot id='4e8VH'></tfoot>
                  <bdo id='4e8VH'></bdo><ul id='4e8VH'></ul>
                  <legend id='4e8VH'><style id='4e8VH'><dir id='4e8VH'><q id='4e8VH'></q></dir></style></legend>
                  本文介绍了消除作为模板参数传递的重载成员函数指针的歧义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在尝试重新创建观察者模式,我可以完美地将参数转发给观察者的给定成员函数.

                  I am attempting to recreate the Observer pattern where I can perfectly forward parameters to a given member function of the observers.

                  如果我尝试传递具有多重覆盖成员函数的地址,它无法根据参数推断出正确的成员函数.

                  If I attempt to pass the address of a member function which has multiple overrides, it cannot deduce the correct member function based on the arguments.

                  #include <iostream>
                  #include <vector>
                  #include <algorithm>
                  
                  template<typename Class>
                  struct observer_list
                  {
                      template<typename Ret, typename... Args, typename... UArgs>
                      void call(Ret (Class::*func)(Args...), UArgs&&... args)
                      {
                          for (auto obj : _observers)
                          {
                              (obj->*func)(std::forward<UArgs>(args)...);
                          }
                      }
                      std::vector<Class*> _observers;
                  };
                  
                  struct foo
                  {
                      void func(const std::string& s)
                      {
                          std::cout << this << ": " << s << std::endl;
                      }
                      void func(const double d)
                      {
                          std::cout << this << ": " << d << std::endl;
                      }
                  };
                  
                  int main()
                  {
                      observer_list<foo> l;
                      foo f1, f2;
                      l._observers = { &f1, &f2 };
                  
                      l.call(&foo::func, "hello");
                      l.call(&foo::func, 0.5);
                  
                      return 0;
                  }
                  

                  这无法编译,模板参数推导/替换失败.

                  请注意,我有 Args...UArgs... 因为我需要能够传递不一定与类型相同的参数函数签名,但可以转换为所述类型.

                  Note that I had Args... and UArgs... because I need to be able to pass parameters which are not necessarily the same type asthe type of the function signature, but are convertible to said type.

                  我想我可以使用 std::enable_if<std::is_convertible<Args, UArgs>> 调用来消除歧义,但我不相信我可以用可变参数来做到这一点模板参数包?

                  I was thinking I could use a std::enable_if<std::is_convertible<Args, UArgs>> call to disambiguate, but I don't believe I can do this with a variadic template parameter pack?

                  我怎样才能让模板参数推导在这里起作用?

                  推荐答案

                  问题在这里:

                  l.call(&foo::func, "hello");
                  l.call(&foo::func, 0.5);
                  

                  对于这两行,编译器不知道您指的是哪个 foo::func.因此,您必须通过强制转换提供缺少的类型信息(即 foo:func 的类型)来消除歧义:

                  For both lines, the compiler doesn't know which foo::func you are referring to. Hence, you have to disambiguate yourself by providing the type information that is missing (i.e., the type of foo:func) through casts:

                  l.call(static_cast<void (foo::*)(const std::string&)>(&foo::func), "hello");
                  l.call(static_cast<void (foo::*)(const double      )>(&foo::func), 0.5);
                  

                  或者,您可以提供编译器无法推导出的模板参数,这些参数定义了 func 的类型:

                  Alternatively, you can provide the template arguments that the compiler cannot deduce and that define the type of func:

                  l.call<void, const std::string&>(&foo::func, "hello");
                  l.call<void, double            >(&foo::func, 0.5);
                  

                  请注意,您必须使用 double 而不是上面的 const double.原因是一般doubleconst double是两种不同的类型.然而,在一种情况下 doubleconst double 被认为是相同的类型:作为函数参数.例如,

                  Notice that you have to use double and not const double above. The reason is that generally double and const double are two different types. However, there's one situation where double and const double are considered as if they were the same type: as function arguments. For instance,

                  void bar(const double);
                  void bar(double);
                  

                  不是两个不同的重载而是实际上是同一个函数.

                  are not two different overloads but are actually the same function.

                  这篇关于消除作为模板参数传递的重载成员函数指针的歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用模板技巧访问私人成员 下一篇:为模板类重载运算符时的隐式转换

                  相关文章

                  最新文章

                  <tfoot id='9NWSx'></tfoot>

                  <small id='9NWSx'></small><noframes id='9NWSx'>

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

                      • <bdo id='9NWSx'></bdo><ul id='9NWSx'></ul>