<legend id='6Mhi5'><style id='6Mhi5'><dir id='6Mhi5'><q id='6Mhi5'></q></dir></style></legend>
    1. <tfoot id='6Mhi5'></tfoot>
      • <bdo id='6Mhi5'></bdo><ul id='6Mhi5'></ul>

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

        <small id='6Mhi5'></small><noframes id='6Mhi5'>

        编译器不推导出模板参数(映射 std::vector -> st

        时间:2023-09-15
        <i id='vohue'><tr id='vohue'><dt id='vohue'><q id='vohue'><span id='vohue'><b id='vohue'><form id='vohue'><ins id='vohue'></ins><ul id='vohue'></ul><sub id='vohue'></sub></form><legend id='vohue'></legend><bdo id='vohue'><pre id='vohue'><center id='vohue'></center></pre></bdo></b><th id='vohue'></th></span></q></dt></tr></i><div id='vohue'><tfoot id='vohue'></tfoot><dl id='vohue'><fieldset id='vohue'></fieldset></dl></div>
      1. <small id='vohue'></small><noframes id='vohue'>

          <tbody id='vohue'></tbody>
          <bdo id='vohue'></bdo><ul id='vohue'></ul>

              • <legend id='vohue'><style id='vohue'><dir id='vohue'><q id='vohue'></q></dir></style></legend>

                  <tfoot id='vohue'></tfoot>
                  本文介绍了编译器不推导出模板参数(映射 std::vector -> std::vector)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有以下模板.

                  template<typename T, typename U>
                  std::vector<U> map(const std::vector<T> &v, std::function<U(const T&)> f) {
                      std::vector<U> res;
                      res.reserve(v.size());
                      std::transform(std::begin(v), std::end(v), std::end(res), f);
                      return res;
                  }
                  

                  当我在代码中使用它时,我指定了模板参数.为什么编译器无法为我推断出这一点?我必须如何更改我的模板定义才能使其正常工作?

                  When I use it in my code I have the specify the template parameters. Why is the compiler not able to deduce this for me? How do I have to change my template definition to make this work?

                  vector<int> numbers = { 1, 3, 5 };
                  
                  // vector<string> strings = map(numbers, [] (int x) { return string(x,'X'); });
                  
                  vector<string> strings = map<int, string>(numbers, [] (int x) { return string(x,'X'); });
                  

                  可运行代码:http://ideone.com/FjGnxd

                  这个问题的原始代码来自这里:标准::transform-like 函数,返回转换后的容器

                  The original code in this question comes from here: The std::transform-like function that returns transformed container

                  推荐答案

                  您的函数需要一个 std::function 参数,但您正在使用 lambda 表达式调用它.两者不是同一类型.lambda 可以转换std::function,但模板参数推导需要完全匹配,并且不考虑用户定义的转换.因此推演失败.

                  Your function expects an std::function argument, but you're calling it with a lambda expression instead. The two are not the same type. A lambda is convertible to std::function, but template argument deduction requires exact matches and user defined conversions are not considered. Hence the deduction failure.

                  如果您实际上将 std::function 传递给 map(),则推导确实有效.

                  Deduction does work if you actually pass an std::function to map().

                  std::function<string(int const&)> fn = [] (int x) { return string(x,'X'); };
                  vector<string> strings = map(numbers, fn);
                  

                  现场演示

                  避免必须指定模板参数的一种可能解决方法是修改函数以接受任何类型的可调用对象,而不是 std::function 对象.

                  One possible workaround to avoid having to specify the template arguments is to modify the function to accept any kind of callable, rather than an std::function object.

                  template<typename T, typename Func>
                  std::vector<typename std::result_of<Func(T)>::type>
                      map(const std::vector<T> &v, Func f) {
                          // ...
                      }
                  

                  相同想法的另一个版本,使用 decltypedeclval 而不是 result_of

                  Another version of the same idea, using decltype and declval instead of result_of

                  template<typename T, typename Func>
                  std::vector<decltype(std::declval<Func>()(std::declval<T>()))>
                      map(const std::vector<T> &v, Func f) {
                          // ...
                      }
                  

                  最后,使用尾随返回类型

                  Finally, using a trailing return type

                  template<typename T, typename Func>
                  auto map(const std::vector<T> &v, Func f) 
                    -> std::vector<decltype(f(v[0]))> {
                          // ...
                      }
                  

                  现场演示

                  这篇关于编译器不推导出模板参数(映射 std::vector -> std::vector)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:向量与字符串 下一篇:C++向量&lt;vector&lt;double&gt;&gt;加倍**

                  相关文章

                  最新文章

                  <tfoot id='z9Pcl'></tfoot>

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

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

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