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,423 of 33,346   
   =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to All   
   Re: Initializer lists vs. 0 or 1 items   
   27 Aug 11 05:55:43   
   
   5b9663a5   
   From: daniel.kruegler@googlemail.com   
      
   Am 27.08.2011 04:24, schrieb Daryle Walker:   
      
   [..]   
      
   > Well, it's a complex-number type, so its implementation would be like   
   > "T c_[2]", so the default construction code would work just fine.   
      
   I haven't read it this way, because why in heaven should you then use an   
   initializer list constructor here? Why not using a normal two-argument   
   constructor with default arguments as in:   
      
   template    
   class my_complex   
   {   
   public:   
        my_complex() = default;   
        my_complex(T first, T second = T());   
   };   
      
   If you prefer to have the defaulted default constructor, otherwise use:   
      
   template    
   class my_complex   
   {   
   public:   
        my_complex(T first = T(), T second = T());   
   };   
      
   But see below for another alternative via delegating constructors.   
      
   > Provided constructor arguments would fill in elements of the array   
   > until the array or list runs out.   
   >   
   > Hmm, there's no way to limit the maximum (or minimum) length of an   
   > initializer list statically, is there?   
      
   Not with initializer lists, but why not using a normal, fixed parameter   
   list instead?   
      
   > Separately, I've heard about sibling constructor calls.  If I want to   
   > minimize duplicated code, I can have my single-argument constructor   
   > reuse the list one:   
   >   
   > template<  typename T>   
   > class my_complex   
   > {   
   > public:   
   >       my_complex( std::initializer_list  list = {} );   
   >       my_complex( T first )   
   >           : my_complex( {first} )   
   >           {}   
   > //...   
   > };   
   >   
   > Right?   
      
   The proper description is a delegating constructor. Yes, this works and   
   you can apply the very same idiom to a fixed parameter list constructor   
   as well, like so:   
      
   template    
   class my_complex   
   {   
   public:   
        my_complex() = default;   
        my_complex(T first, T second);   
        my_complex(T first) : my_complex(first, T()) {}   
   };   
      
   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