home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 33,172 of 33,346   
   =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to Daryle Walker   
   Re: How to fill in a compile-time (but n   
   14 Aug 13 14:36:49   
   
   From: daniel.kruegler@googlemail.com   
      
   On 2013-08-05 03:44, Daryle Walker wrote:   
   > If the size of an array is fixed in the source text, it's easy to   
   > define it:   
   >   
   >      struct test1   
   >      {   
   >          static constexpr   
   >          int  my_data[] = { 1, 2, 3, 4 };   
   >      };   
   >   
   >      constexpr   
   >      int  test1::my_data[ 4 ];   
   >   
   > But what if the array's size is determined by an algorithm, still fixed   
   > in compile-time.  Can it be written?   
      
   Yes.   
      
   >      template < int X >   
   >      struct my_meta_func   
   >          : std::integeral_constant   
   >      { };   
   >   
   >      template < std::size_t L >   
   >      struct test2   
   >      {   
   >          static constexpr   
   >          int  my_data[] = { my_meta_func<0>::value,   
   >           my_meta_func<1>::value, ..., my_meta_func::value };   
   >          // How do I actually fill in the above line?   
   >      };   
   >   
   >      template < std::size_t L >   
   >      int  test2::my_data[ L ];   
   >   
   > I (mostly) have no clue how to do this.  Right before writing this, I   
   > came up with the C++11 trick of appending (C++) varargs to a variadic   
   > (function) template, then using that list to initialize an array when   
   > it's long enough.  Will that work?   
      
   I have not fully grasped your suggestion here (Especially it is unclear   
   to me what you mean by 'list' in this context), but given the following   
   tools   
      
   template   
   struct index_sequence   
   {   
     static constexpr std::size_t size() { return sizeof...(N); }   
   };   
      
   template   
   struct make_index_sequence_impl;   
      
   template   
   struct make_index_sequence_impl, N>   
   {   
     typedef typename make_index_sequence_impl, N>::type type;   
   };   
      
   template   
   struct make_index_sequence_impl, N>   
   {   
     typedef index_sequence type;   
   };   
      
   template   
   using make_index_sequence = typename make_index_sequence_impl<0,   
   index_sequence<>, N>::type;   
      
   this is not too hard to implement, e.g. you could write:   
      
   template   
   struct test2_base;   
      
   template   
   struct test2_base>   
   {   
     static constexpr int my_data[] = { my_meta_func::value... };   
   };   
      
   template   
   constexpr int   
   test2_base>::my_data[index_sequence::size()];   
      
   template   
   struct test2 : test2_base>   
   {   
   };   
      
   Note that for C++14, the templates index_sequence and   
   make_index_sequence are already provided by the Standard Library in   
   namespace std.   
      
   > Can a multi-dimensional array be   
   > filled with initialization of a flat list?  (That works in non-meta   
   > contexts.)   
      
   Sure, why not?   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca