<bdo id='nLTyZ'></bdo><ul id='nLTyZ'></ul>

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

  • <tfoot id='nLTyZ'></tfoot><legend id='nLTyZ'><style id='nLTyZ'><dir id='nLTyZ'><q id='nLTyZ'></q></dir></style></legend>

        在函数模板特化中覆盖返回类型

        时间:2023-05-24
        <legend id='ju8s0'><style id='ju8s0'><dir id='ju8s0'><q id='ju8s0'></q></dir></style></legend>
        • <bdo id='ju8s0'></bdo><ul id='ju8s0'></ul>

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

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

                <tbody id='ju8s0'></tbody>

                  本文介绍了在函数模板特化中覆盖返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我想专门化一个函数模板,以便返回类型根据模板参数的类型而变化.

                  I would like to specialize a function template such that the return type changes depending on the type of the template argument.

                  class ReturnTypeSpecialization
                  {
                  public:
                      template<typename T>
                      T Item();
                  };
                  
                  // Normally just return the template type
                  template<typename T>
                  T ReturnTypeSpecialization::Item() { ... }
                  
                  // When a float is specified, return an int
                  // This doesn't work:
                  template<float>
                  int ReturnTypeSpecialization::Item() { ... }
                  

                  这可能吗?我不能使用 C++11.

                  Is this possible? I can't use C++11.

                  推荐答案

                  由于专业化必须与返回类型的基本模板一致,您可以通过添加返回类型特征"来实现,您可以使用一个结构专门化并从以下位置绘制真正的返回类型:

                  Since the specialization has to agree with the base template on the return type, you can make it so by adding a "return type trait", a struct you can specialize and draw the true return type from:

                  // in the normal case, just the identity
                  template<class T>
                  struct item_return{ typedef T type; };
                  
                  template<class T>
                  typename item_return<T>::type item();
                  
                  template<>
                  struct item_return<float>{ typedef int type; };
                  template<>
                  int item<float>();
                  

                  现场示例.

                  请注意,您可能希望遵循以下规则,因此您只需更新 item_return 专业化中的 return-type.

                  Note that you might want to stick to the following, so you only need to update the return-type in the item_return specialization.

                  template<>
                  item_return<float>::type foo<float>(){ ... }
                  // note: No `typename` needed, because `float` is not a dependent type
                  

                  这篇关于在函数模板特化中覆盖返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Lambda 和 std::function 下一篇:何时使用模板 vs 继承

                  相关文章

                  最新文章

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

                1. <small id='pgDzg'></small><noframes id='pgDzg'>

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