c++ - array of arrays of different size -
I have some code that sets one of different sizes of tr1 :: array, but the same type, like < / P>
Array & lt; Int, 2 & gt; Array & lt; Int, 4 & gt; Array & lt; Int, 6 & gt;
The numbers of these arrays, and their size, time are given in the compilation, so I know how many of them will be and how big they are (but they May be different).
The problem: I want to put them in a collection (using the array & gt; would be great), but for all the members the type should be identical and this is not the case.
I thought about using the Promotion: version, but how can I specify a type with compiled time determined list (I'm thinking of heavy use of preprocessor. ..)? About using promotion: Anyone? Other methods? (Wild pointer?)
TIA ~ AKI
Correction: Preprocessor is not usable in this case.
I will use Boost's MPL and Fusion Library to have two ways to end with the I type list. : Generate them, or explicitly define them. The former is slightly more flexible, but it's hard to say what's right for you because we do not know which values you have.
In any case, to create:
#include & lt; Boost / mpl / for_each.hpp & gt; # Include & lt; Boost / mpl / range_c.hpp & gt; #include & lt; Boost / mpl / transform.hpp & gt; # Include & lt; Boost / mpl / vector.hpp & gt; # Include & lt; Array & gt; # Include & lt; Iostream & gt; Name Location bmpl = boost :: mpl; // turns an index into an array template & lt; Typename T & gt; Struct make_array {// or whatever plan gives you constant const. Std :: size_t size = t :: value * 2; // defined type type typedef std :: array & lt; Int, size & gt; type; }; Type a list of values to type: bmpl :: range_c & lt; Size_t, 1, 10 & gt; Array_range; // change that list into arrays, a vector typepeed BMPL :: Transform & Lieutenant; Array_range, makere and lieutenant; BMPL :: _1 & gt; BMPL :: Back_Incenter & lt; BMPL :: Vector & lt; & Gt; & Gt; & Gt; :: type arrow_collection;
Or explicitly stating:
#include & lt; Boost / mpl / vector.hpp & gt; # Include & lt; Array & gt; # Include & lt; Iostream & gt; Name Location bmpl = boost :: mpl; // Type all array types bpmpl :: vector & lt; Std :: array & lt; Int, 2>, std :: arrays & lt; Int, 4>, std :: arrays and lt; Int, 6 & gt; std :: arrays & lt; Int, 8 & gt;, std :: array & lt; Int, 10 & gt; Std :: array & lt; Int, 12>, std :: arrays & lt; Int, 14 & gt; std :: arrays & lt; Int, 16 & gt; std :: array & lt; Int, 18 & gt; & Gt; Array_collection; By any means, you can use it like this: #include & lt; Boost / fusion / algorithm.hpp & gt; #include & lt; Boost / fusion / container / vector.hpp & gt; #include & lt; Boost / fusion / mpl.hpp & gt; #include & lt; Boost / fusion / sequence.hpp & gt; #include & lt; Boost / mpl / for_each.hpp & gt; #include & lt; Typeinfo & gt; // Fusion "Fuses" MPL and Runtime Namespot BF = Boost :: Bridge between Fusion; Struct print_type {template & lt; Typename T & gt; Zero operator () (CONST T & amp;) const {std :: cout & lt; & Lt; Typid (t) .name () & lt; & Lt; "\ N"; }}; Struct print_values {template & lt; Typename T & gt; Zero operator () (CONST T & PRRAR) const {std :: cout & lt; & Lt; "Printing array with size" & lt; & Lt; PArray.size () & lt; & Lt; ": \ N"; Std :: for_each (pArray.begin (), pArray.end (), [] (int pX) {std :: cout & lt; & lt; px & lt; & lt; "";}); Std :: cout & lt; & Lt; Std :: endl; }}; Int main (zeros) {// All types of bmpl :: for_each & lt; Array_collection & gt; (Print_type ()); Std :: cout.flush (); // Type Type Beef :: Results_of :: as_vector & lt; Array_collection & gt; :: type array_fusion; Use a useful type out of; Array_fusion arrays; // is now an array of different arrays, // compile-time generated, but run-time usable // like this: bf :: at_c & lt; 0 & gt; Arrays [1] = 5; Bf :: at_c & lt; 1 & gt; (Array) [2] = 7; Bf :: at_c & lt; 2 & gt; (Array) [0] = 135; // for_each: bf :: for_each (arrays, print_values ()); }
Comments
Post a Comment