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

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

          <bdo id='9RcE9'></bdo><ul id='9RcE9'></ul>

        带有模板参数的模板中的默认值 (C++)

        时间:2023-05-24
        • <small id='ILil0'></small><noframes id='ILil0'>

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

                1. <tfoot id='ILil0'></tfoot>
                    <tbody id='ILil0'></tbody>
                  <i id='ILil0'><tr id='ILil0'><dt id='ILil0'><q id='ILil0'><span id='ILil0'><b id='ILil0'><form id='ILil0'><ins id='ILil0'></ins><ul id='ILil0'></ul><sub id='ILil0'></sub></form><legend id='ILil0'></legend><bdo id='ILil0'><pre id='ILil0'><center id='ILil0'></center></pre></bdo></b><th id='ILil0'></th></span></q></dt></tr></i><div id='ILil0'><tfoot id='ILil0'></tfoot><dl id='ILil0'><fieldset id='ILil0'></fieldset></dl></div>
                  <legend id='ILil0'><style id='ILil0'><dir id='ILil0'><q id='ILil0'></q></dir></style></legend>
                  本文介绍了带有模板参数的模板中的默认值 (C++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  假设我有一个模板(称为 ExampleTemplate),它接受两个参数:容器类型(例如列表、向量)和包含类型(例如 float、bool 等).由于容器实际上是模板,因此该模板具有模板参数.这是我必须写的:

                  Assume I have a template (called ExampleTemplate) that takes two arguments: a container type (e.g. list, vector) and a contained type (e.g. float, bool, etc). Since containers are in fact templates, this template has a template param. This is what I had to write:

                  #include <vector>
                  #include <list>
                  
                  using namespace std;
                  
                  template < template <class,class> class C, typename T>
                  class ExampleTemplate {
                      C<T,allocator<T> > items;
                  public:
                      ....
                  };
                  
                  main()
                  {
                      ExampleTemplate<list,int> a;
                      ExampleTemplate<vector,float> b;
                  }
                  

                  您可能会问什么是分配器".嗯,最初,我尝试了显而易见的事情......

                  You may ask what is the "allocator" thing about. Well, Initially, I tried the obvious thing...

                  template < template <class> class C, typename T>
                  class ExampleTemplate {
                      C<T> items;
                  };
                  

                  ...但不幸的是我发现分配器的默认参数...

                  ...but I unfortunately found out that the default argument of the allocator...

                     vector<T, Alloc>
                     list<T, Alloc>
                     etc
                  

                  ...必须在模板声明中明确保留".如您所见,这使代码变得更丑陋,并迫使我重现模板参数(在本例中为分配器)的默认值.

                  ...had to be explicitely "reserved" in the template declaration. This, as you can see, makes the code uglier, and forces me to reproduce the default values of the template arguments (in this case, the allocator).

                  哪个是.

                  问题不是关于容器的具体问题——而是关于带有模板参数的模板中的默认值",上面只是一个例子.取决于 STL 容器具有::value_type"的知识的答案不是我所追求的.想想一般的问题:如果我需要在模板ExampleTemplate中使用模板参数C,那么在ExampleTemplate的主体中,我使用时是否必须重现C的默认参数?如果我不得不这样做,会不会引入不必要的重复和其他问题(在这种情况下,C 是 STL 容器,可移植性问题 - 例如分配器")?

                  The question is not about the specific problem of containers - it is about "Default values in templates with template arguments", and the above is just an example. Answers depending on the knowledge that STL containers have a "::value_type" are not what I am after. Think of the generic problem: if I need to use a template argument C in a template ExampleTemplate, then in the body of ExampleTemplate, do I have to reproduce the default arguments of C when I use it? If I have to, doesn't that introduce unnecessary repetition and other problems (in this case, where C is an STL container, portability issues - e.g. "allocator" )?

                  推荐答案

                  也许你更喜欢这个:

                  #include <vector>
                  #include <list>
                  
                  using namespace std;
                  
                  template <class Container>
                  class ForExamplePurposes {
                      typedef typename Container::value_type T;
                      Container items;
                  public:
                  };
                  
                  int main()
                  {
                      ForExamplePurposes< list<int> > a;
                      ForExamplePurposes< vector<float> > b;
                  }
                  

                  这使用静态鸭子打字".它也更加灵活,因为它不强制容器类型支持 STL 的分配器概念.

                  This uses "static duck typing". It is also a bit more flexible as it doesn't force the Container type to support STL's Allocator concept.

                  也许使用 type traits 习语可以给你一个出路:

                  Perhaps using the type traits idiom can give you a way out:

                  #include <vector>
                  #include <list>
                  
                  using namespace std;
                  
                  struct MyFunkyContainer
                  {
                      typedef int funky_type;
                      // ... rest of custom container declaration
                  };
                  
                  // General case assumes STL-compatible container
                  template <class Container>
                  struct ValueTypeOf
                  {
                      typedef typename Container::value_type type;
                  };
                  
                  // Specialization for MyFunkyContainer
                  template <>
                  struct ValueTypeOf<MyFunkyContainer>
                  {
                      typedef MyFunkyContainer::funky_type type;
                  };
                  
                  
                  template <class Container>
                  class ForExamplePurposes {
                      typedef typename ValueTypeOf<Container>::type T;
                      Container items;
                  public:
                  };
                  
                  int main()
                  {
                      ForExamplePurposes< list<int> > a;
                      ForExamplePurposes< vector<float> > b;
                      ForExamplePurposes< MyFunkyContainer > c;
                  }
                  

                  想要将 ForExamplePurposes 与非 STL 兼容容器一起使用的人需要专门化 ValueTypeOf 特征类.

                  Someone who wants to use ForExamplePurposes with a non-STL-compliant container would need to specialize the ValueTypeOf traits class.

                  这篇关于带有模板参数的模板中的默认值 (C++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:GCC 错误:非命名空间范围内的显式特化 下一篇:C++ 模板在 .h 中声明,在 .hpp 中定义

                  相关文章

                  最新文章

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

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

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