Thursday, 15 January 2015

c++ - Template template parameters - extension to std::array -


I have a question about template template parameters:

We consider the following classes:

  Template & lt; Typename t, template & lt; Class, class = std :: allocator & lt; T & gt; & Gt; Class built-in container type = std :: vector & gt; Class MyContainer {Public: T Value1; T value 2; UnderlyingContainerType & LT; T & gt; Container; MyContainer () {} / * Methods implementation goes here;  

In this example, a container chat uses an inherent STL-compatible container that does anything stuff.

By allowing the built-in container type to be a template template parameter, instead of the regular template argument, the class allows for the use of MyContainer, such as:

  MyContainer & Lt; Int & gt; MCV; // Vector Vector Micrintendent & lt; Int, std :: list & gt; MCL; // No need to write std :: list & lt; Int & gt; // Thanks for the template template parameter  

Now, while it will work perfectly with most STL containers, such as std :: vector, std :: deque, std :: list And further, this will not work, for example, with std :: arrays provided in c ++ 11.

The reason for this is that the template parameters from the vector in std :: array are a different signature. Specifically, they are:

  std :: vector & lt; Square t, square allocation = std :: allocation & lt; T & gt; & Gt; Std :: array & lt; Square t, int n & gt;  

My question is whether the class is a way of making Macintnerer normal, so that the built-in container can also be std :: arrays.

I thank you in advance.

Similarities between the vector and array of interfaces Elements are limited to type. Your container should indicate that:

  template & lt; Typename t, template & lt; Typename & gt; Class built-in container & gt; Stretch container {built-in container & lt; T & gt; Storage; };  

Usage now requires a small move:

  template & lt; Typename T & gt; By using Vec = vector & lt; T & gt; Template & lt; Typename T & gt; Arr2 = array & lt; T, 2 & gt;  

Note that, unlike vec , vector , there is a fixed allocation and that is arr2 , < There is a fixed size of code> array .

Usage is now simple:

  container & lt; Int, vec & gt; a; Container & lt; Double, AR2 and gt; B;  

Alternatively, if you prefer to match the interface used by vector, then just for array Add a template nickname size and add an unused type parameter:

  template & lt; Typename t, typename & gt; Arr2 = array & lt; T, 2 & gt;  


No comments:

Post a Comment