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

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

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

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

        C++ 重载函数作为模板参数

        时间:2023-05-25

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

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

        • <tfoot id='neKm8'></tfoot>

              <tbody id='neKm8'></tbody>

            <legend id='neKm8'><style id='neKm8'><dir id='neKm8'><q id='neKm8'></q></dir></style></legend>
              <bdo id='neKm8'></bdo><ul id='neKm8'></ul>

                1. 本文介绍了C++ 重载函数作为模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我的代码的简化版本在这里

                  the simplified version of my code is here

                  int foo(int x)
                  {
                    return x;
                  }
                  
                  int foo(int x, int y)
                  {
                    return x+y;
                  }
                  
                  template<typename unary_func>
                  int bar(int k, unary_func f)
                  {
                    return f(k);
                  }
                  
                  int main()
                  {
                    bar(3, foo);
                    return 0;
                  }
                  

                  有没有办法告诉编译器我想传递的参数是第一个‘foo’?

                  Is there a way to tell the compiler what I want to pass as argument is the first `foo'?

                  推荐答案

                  你可以给出一个明确的模板参数:

                  You can give an explicit template argument:

                  bar<int(int)>(3, foo);
                  

                  或将模棱两可的函数名称强制转换为可以从中推导出模板参数的类型:

                  or cast the ambiguous function name to a type from which the template argument can be deduced:

                  bar(3, static_cast<int(*)(int)>(foo));
                  

                  或将其包装在另一个函数(或函数对象)中以消除歧义

                  or wrap it in another function (or function object) to remove the ambiguity

                  bar(3, [](int x){return foo(x);});
                  

                  这篇关于C++ 重载函数作为模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如果我只想在模板中专门化一种方法,我该怎么 下一篇:C++:友元声明‘声明一个非模板函数

                  相关文章

                  最新文章

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

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