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,343 of 33,346   
   Francis Glassborow to PiotrN   
   Re: std::vector: Surprising order of des   
   31 May 12 12:25:29   
   
   1b71ea81   
   From: francis.glassborow@btinternet.com   
      
   On 31/05/2012 05:20, PiotrN wrote:   
   > Hello,   
   >   
   > I am curious is there any C++ standard rules about destruction of   
   > std::vector elements. For arrays it applies “reverse order” rule –   
   > order of destruction is reverse to elements construction –  and it is   
   > in reverse order of indexes.   
   > It seems, that, for at least one compiler which I tested, the order of   
   > destruction in std::vector is in natural order of indexes, which is   
   > very strange to me!   
   >   
      
   The elements of an array are in a fixed order (though it is possible to   
   move them around by , for example, using a swap function. More   
   importantly, they are also in a fixed location and it takes a deliberate   
   (hopefully conscious) action of the programmer to relocate either the   
   whole array or shuffle the members,  As a general rule it is safe to   
   destroy the elements in reverse order of construction. However, note   
   that if the programmer does use a swap() function or similar on elements   
   of an array that reference each other then undefined behaviour is quite   
   probable.   
      
   In the case of a vector, the design anticipates reordering and growth   
   (and your vector would become unstable simply through growth because the   
   first element would move when growth required relocation of the whole   
   vector). Indeed any vector that includes such internal references is an   
   accident waiting to happen.   
      
   Against that background it seems unreasonable to require that a vector   
   keeps track of the order of construction of its members (that could get   
   quite complex and impose an overhead on the management of every vector).   
   If we are not going to track the order of construction there seems no   
   reason to require destruction in any particular order. ISTM that   
   destructing in index order is just as reasonable as destructing in   
   reverse index order as neither would normally be in reverse order of   
   construction.   
      
      
   --   
         [ 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