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 31,857 of 33,346   
   Marc to Daryle Walker   
   Re: Initializing an array non-static mem   
   26 Jan 12 23:45:39   
   
   feadd3c8   
   From: marc.glisse@gmail.com   
      
   Daryle Walker  wrote:   
      
   > template < typename T, unsigned N >   
   > class my_math_vector   
   > {   
   > public:   
   >     constexpr my_math_vector() : c{} {}   
   >     constexpr my_math_vector( T const &first ) : c{first} {}   
   >     constexpr my_math_vector( T const &val, unsigned index );   
   >       // make this->c[index] == val, all other this->c[x] == T()   
   >     //...   
   > private:   
   >     T  c[ N ];   
   > };   
   >   
   > It's easy to fill in an array from the front during a constexpr constructor.   
   > Is it possible to initialize a single array element that is NOT the first   
   > one?  You can't know statically how many zero (or "T()") to put before your   
   > desired value, which prevents a direct solution.  But maybe there's some   
   > sort of recursive template metaprogramming that can do it?...   
      
   (completely untested)   
   You can delegate to:   
   template constexpr   
   my_math_vector(T const&val,unsigned index,index_type)   
   : c{((I==index)?val:T())...} {}   
      
   by passing an index_type<0,1,...,N-1>.   
      
   I am afraid the copies/moves will not be elided though. A factory with   
   index passed as a template argument might work better.   
      
      
   --   
         [ 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