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

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

      <legend id='seiYp'><style id='seiYp'><dir id='seiYp'><q id='seiYp'></q></dir></style></legend>
      1. 模板是什么意思?在 C++ 中使用空尖括号?

        时间:2023-05-25

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

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

              • <tfoot id='UcgZk'></tfoot>
                  <tbody id='UcgZk'></tbody>

                  本文介绍了模板是什么意思?在 C++ 中使用空尖括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..
                  template<>
                  class A{
                  //some class data
                  };
                  

                  这种代码我见过很多次了.上面代码中template<>的作用是什么?在哪些情况下我们需要强制使用它?

                  I have seen this kind of code many times. what is the use of template<> in the above code? And what are the cases where we need mandate the use of it?

                  推荐答案

                  template<> 告诉编译器遵循模板特化,特别是完全特化.通常,class A 应该是这样的:

                  template<> tells the compiler that a template specialization follows, specifically a full specialization. Normally, class A would have to look something like this:

                  template<class T>
                  class A{
                    // general implementation
                  };
                  
                  template<>
                  class A<int>{
                    // special implementation for ints
                  };
                  

                  现在,每当使用 A 时,都会使用专用版本.你也可以用它来专门化函数:

                  Now, whenever A<int> is used, the specialized version is used. You can also use it to specialize functions:

                  template<class T>
                  void foo(T t){
                    // general
                  }
                  
                  template<>
                  void foo<int>(int i){
                    // for ints
                  }
                  
                  // doesn't actually need the <int>
                  // as the specialization can be deduced from the parameter type
                  template<>
                  void foo(int i){
                    // also valid
                  }
                  

                  通常情况下,你不应该专门化函数,因为简单的重载通常被认为是优越的:

                  Normally though, you shouldn't specialize functions, as simple overloads are generally considered superior:

                  void foo(int i){
                    // better
                  }
                  

                  <小时>

                  现在,为了让它显得矫枉过正,以下是一个部分专业化:

                  template<class T1, class T2>
                  class B{
                  };
                  
                  template<class T1>
                  class B<T1, int>{
                  };
                  

                  与完全特化的工作方式相同,只是当第二个模板参数是 int(例如,BB 等).

                  Works the same way as a full specialization, just that the specialized version is used whenever the second template parameter is an int (e.g., B<bool,int>, B<YourType,int>, etc).

                  这篇关于模板是什么意思?在 C++ 中使用空尖括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:c++ 模板类;具有任意容器类型的函数,如何定义 下一篇:基于继承类的模板特化

                  相关文章

                  最新文章

                • <small id='xgNiv'></small><noframes id='xgNiv'>

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

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

                      <tfoot id='xgNiv'></tfoot>

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