c++ - Fill container with template parameters -


I definitely want to fill the template parameters in length with an array in the variadic template. For that purpose I have the following helper function templates

  template & lt; Typename ForwardIterator, typename T & gt; Fill zero (forward iterator i) {} template & lt; Typename ForwardIterator, typename t, t head, t ... tail & gt; Zero fill (forward iterator i) {* i = head; Fill & lt; Forwarded Eater, T, Tail ... & gt; (++i); }  

The following class template

  template & lt; Typename t, t ... args & gt; Struct params_to_array; Template & lt; Typename t, t final & gt; Struct params_to_array & lt; T, final & gt; {Static const std :: size_t size = 1; Typedef std :: array & lt; T, SIZE & gt; Array_type; Static constant array_type parameter; Private: Zero init_params () {array_type results; Fill & lt; Typename array_type :: iterator, T, head, tail ... & gt; (Result.begin ()); Return result; }}; Template & lt; Typename t, t head, t ... tail & gt; Struct params_to_array & lt; T, head, tail ... & gt; {Static const std :: size_t size = params_to_array & lt; T, tail ... & gt; :: SIZE + 1; Typedef std :: array & lt; T, SIZE & gt; Array_type; Static constant array_type parameter; Private: Zero init_params () {array_type results; Fill & lt; Typename array_type :: iterator, T, last & gt; (Result.begin ()); Return result; }};  

and

  template & lt; Typename t, t last & gt; Fixed constant was initiated through Constant TypeName_Love_Learn & lt; T, last & gt; :: array_type param_to_array & gt; T, last & gt; :: params = param_to_array & lt; T, final & gt; :: init_params ();  

and

  template & lt; Typename t, t head, t ... tail & gt; T, head, tail ... & gt; :: Parameters = param_to_array & lt; T, head, tail ... & gt; :: init_params ();  

Now the array

  param_to_array & lt; Int, 1, 3, 4 & gt; :: params  

is a std :: array and lt; Int, 3 & gt; and it contains the value 1 , 3 and 4 . I think there should be a simple way of achieving this behavior. any suggestion?

Edit: As N. Roberts has suggested in my reply, I modified my program in the following: I have written a new structure, which is the element in the parameter list Counts:

template & lt; Typename t, t ... args & gt; Composition calculation; Template & lt; Typename t, t head, t ... tail & gt; Structure calculation & lt; T, head, tail ... & gt; {Static const std :: size_t value = count & lt; T, tail ... & gt; Value + 1; }; Template & lt; Typename t, t final & gt; Stuct count & lt; T, last & gt; {Static const std :: size_t value = 1; };

and the following functions are written

  template & lt; Typename t, t ... args & gt; Std :: array & lt; T, count & lt; T, args ... & gt;: Price & gt; Params_to_array () {std :: arrays & lt; T, count & lt; T, args ... & gt; :: Price & gt; result; Fill & lt; Typename std :: array & lt; T, count & lt; T, args ... & gt; :: value & gt; :: iterator, T, args ... & gt; (Result.begin ()); Return result; }  

Now I

  params_to_array  

A 10 , 20 and 30 with std :: arrays and lt; Int, 3 & gt; Any other suggestions?

There is no need to manually calculate the type of number in a parameter pack > Sizeof ... is for operator except for this I will create an Iterator type for fill , there is no need to explicitly specify it:

  template & lt; Typename T, typename FwdIt & gt; Zero Fill (FwdIt this) {} Template & lt; Typename t, t head, t ... tail, typename fwdIt & gt; Zero fill (FwdIt this) {* it = head; Fill & lt; T, tail ... & gt; (++); } Template & lt; Square t, t ... args & gt; Std :: array & lt; T, sizeof ... (args) & gt; Params_to_array () {std :: arrays & lt; T, sizeof ... (args) & gt; a; Fill & lt; T, args ... & gt; (A.begin ()); Return; };  

The parameter pack, although initializer-list references are also expandable, which makes fill unnecessary: ​​

  Template & lt; Class t, t ... args & gt; Std :: array & lt; T, sizeof ... (args) & gt; Params_to_array () {std :: arrays & lt; T, sizeof ... (args) & gt; A = {{Args ...}}; Return; };  

Comments