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 32,177 of 33,346   
   =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to All   
   Re: Why no conversion from std::initiali   
   22 Apr 12 17:15:55   
   
   From: daniel.kruegler@googlemail.com   
      
   Am 22.04.2012 22:00, schrieb Ivan Godard:   
   [..]   
   > I guess I'm asking: in any context in which an aggregate initializer   
   > is accepted (e.g. "int arr[2] = {1, 2}", couldn't the language accept   
   > an expression of type initializer_list with the meaning that the   
   > list is treated as an aggregate, all of whose elements are of type T   
   > with values taken consecutively from the list? That is, "int arr[2] =   
   > i" is not taken as an initializer-list constructor of arr (arrays   
   > can't be assigned) but as a way of representing an aggregate for a   
   > normal aggregate assignment.   
      
   Yes, that would work, once you ensure that std::initializer_list has a   
   constexpr size() function (or is at least aware of it's size in an   
   equivalent way) *and* if the source initializer_list is a constant   
   expression. Note that current aggregate initializers are constexpr in   
   regard to the size already and any mismatch is caught during   
   compile-time. Or are you suggesting something comparable to   
   array-new-expressions that are initialized with an initializer-list   
   (Those would need to throw a std::bad_array_new_length in case of length   
   exceeding)   
      
   > It wouldn't work for heterogeneous structs of course, because there   
   > are no heterogeneous initializer lists. Pity.   
      
   Another complication with your suggestion is that it would be hard to   
   explain to user-code that this works:   
      
   constexpr auto li = {1, 2, 3}; // OK, li is std::initializer_list   
   int arr[] = li; // OK, would work under your proposal   
      
   But this cannot be be made working:   
      
   constexpr auto ls = {std::string("a"), std::string("b")}; // Error,   
     // cannot be a constant expression   
   auto ls2 = {std::string("a"), std::string("b")}; // OK, but no constant   
   expression   
   std::string ars[] = ls2; // Error, no constant initializer,   
     // how do we determine the statically known length of the array?   
      
   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