c++ - Storing set of std::arrays of any (but constant) length -


is there way store set of std::arrays of (but constant) length length of array can later used in constexpr?

i guess standard containers out of question, there might template solution somehow. of information available @ compile time, isn't it?

example code:

#include <iostream> #include <string> #include <vector> #include <array> #include <algorithm>  // class storing array of (but constant) size struct {     const std::vector<std::string> container;      const int container_size;     a(const std::vector<std::string>& v, int size) : container(v), container_size(size) { } };  int main() {     // list of variable length const arrays , sizes     std::vector<a> myalist {         a({ std::string("string1"), std::string("string2") }, 2),         a({ std::string("string1") }, 1)     };      // how go using size of each array in constexpr?     (auto const& : myalist) {     // example constexpr:     //  somefunc(std::make_index_sequence<a.container_size>{});      // 2nd example converting actual std::array     //  std::array<std::string, a.container_size> arr;     //  std::copy_n(std::make_move_iterator(a.begin()), a.container_size, arr.begin());     }      return 0; } 

update:

more details requested, here goes. don't care how array defined, works... exact constexpr used 1 in example code, std::make_index_sequence<constexpr>{}. i know have set of constant arrays defined @ compile time, , should somehow possible refer length elsewhere in constexpr.

heck, i'd fine storing lengths:

// class storing array size struct {     a(int size) : container_size(size) { }     const int container_size; };  int main() {     // list of lengths     std::vector<a> mysizelist { 2, 1 };      (auto const& : mysizelist) {     //  somefunc(std::make_index_sequence<a.container_size>{});     }     return 0; } 

this assumes want call somefunc both const& std::vector , index_sequence, , want call somefunc. offers support somefunc extended signature (simply pass 3rd argument return value and/or additional arguments, passed after std::vector<t> const&).

failure align size of vector size passed in result in badness if size passed in longer vector.

it assumes know function calling @ point of construction. can generalized finite set of functions want call @ point of construction, naturally.

the technique used called "type erasure" or "run-time concepts". erase down concept of invoking some_func index sequence in question during construction, , store erased operation in f.

template<size_t n> using size=std::integral_constant<size_t, n>;  template<   class t, template<class...>class operation,   class sig=std::result_of_t<operation<size<0>>(std::vector<t> const&)>() > struct eraser;  template<class t, template<class...>class operation, class r, class...args> struct eraser<t,operation, r(args...)> {   std::vector<t> data;   r(*f)(eraser const*, args&&...);   r operator()(args...args)const{     return f(this, std::forward<args>(args)...);   }   template<size_t n>   eraser( std::initializer_list<t> il, size<n> ):     eraser( il.begin(), size<n>{} )   {}   template<class...ts>   eraser( t t0, ts... ts ):     eraser(       {std::forward<t>(t0), std::forward<ts>(ts)...},       size<sizeof...(ts)+1>{}     )   {}   template<size_t n>   eraser( t const* ptr, size<n> ):     data(ptr, ptr+n),     f([](eraser const*self, args&&...args)->r{       return operation<size<n>>{}(self->data, std::forward<args>(args)...);     })   {} }; template<class t, size_t ... is> void some_func( std::vector<t> const&, std::index_sequence<is...>) {   std::cout << "called! [#" << sizeof...(is) << "]\n"; } template<class n> struct call_some_func {   template<class t>   void operator()(std::vector<t> const& v) const {     some_func( v, std::make_index_sequence<n{}>{} );   } };   int main() {   using = eraser<std::string, call_some_func>;   // list of variable length const arrays , sizes   std::vector<a> myalist {     a({ std::string("string1"), std::string("string2") }, size<2>{}),     a({ std::string("string1") }, size<1>{})   };    std::cout << "objects constructed.  loop start:\n";    // how go using size of each array in constexpr?   (auto const& : myalist) {     a();   } } 

live example


Comments