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,181 of 33,346    |
|    Dave Abrahams to All    |
|    Re: Initialization and trivial construct    |
|    23 Apr 12 09:18:10    |
   
   From: dave@boostpro.com   
      
   on Sun Apr 22 2012, brangdon-AT-cix.compulink.co.uk (Dave Harris) wrote:   
      
   > { Article reformatted; please limit your lines to 70 charaters -mod }   
   >   
   > roberts.noah@gmail.com (nroberts) wrote (abridged):   
   >> Say you were given a coding policy that stated, "Only initialize   
   >> members with non-trivial constructors and destructors in your   
   >> constructor's initialization list." Assuming they mean what they   
   >> are saying, and understand the difference between trivial and   
   >> non-trivial constructors, could there be a reasonable explanation   
   >> for this policy?   
   >   
   > They may be trying to say that:   
   >   
   > Class::Class() : a(0), b(0), c(0), d(0) {   
   > }   
   >   
   > would be better written as:   
   >   
   > Class::Class() {   
   > a = 0;   
   > b = 0;   
   > c = 0;   
   > d = 0;   
   > }   
   >   
   > The former may be more efficient if the members have code in their   
   > constructors, but if they are just ints, then the efficiency will be   
   > about the same. The latter version is arguably easier to read, write   
   > and edit.   
      
   I think Dave H. is being overly generous to the designers of such a   
   policy. IMO the disadvantages of putting things in the constructor body   
   that could go in the initialization list outweigh the advantages by a   
   very significant margin.   
      
   As soon as you put something in the constructor body, you've introduced   
   mutation, which makes code much harder to reason about. Objects   
   constructed in the initializer list don't exist until they're   
   initialized, so there is no mutation. You've also introduced semantics   
   that can't be automatically reversed if there's an exception.   
      
   Like the use of "hungarian notation", the policy makes the details of   
   code sensitive to the specific types used, rather than to the concepts   
   they model---should I really have to rewrite constructors if I   
   replace   
      
    typedef double fraction;   
      
   with   
      
    typedef boost::rational
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca