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,594 of 33,346    |
|    Cassio Neri to All    |
|    Re: Move, std::vector and gcc: which is     |
|    18 Oct 12 12:57:55    |
      From: cassio.neri@googlemail.com              > In other words, when both copy and move are defined explicitly, the       > copy constructor gets called but if the move constructor is       > defaulted, then it gets called in preference to the copy       > constructor.       >       > I would have expected the GCC 4.6 behaviour to be correct. Am I       > mistaken?              I think GCC 4.7 is correct and GCC 4.6 is not. I also believe (to be       checked) that the issue here is exception safety. When you increases       the vector capacity the elements must be copied or moved to the new       memory buffer. Let's consider the two possibilities:              1) Elements are copied.              That is, each element is copied into the new buffer. Suppose the       vector has successfully copied the first element to the new buffer and       now is copying the second element. What happens, from the exception       point of view, if the copy constructor throws? Only a copy of the       first element was made and the original is still there. Then, the       vector can deliver the strong exception guarantee.              2) Elements are moved.              That is, each element is moved into the new buffer. Suppose the vector       has successfully moved the first element to the new buffer and now is       moving the second element. What happens, from the exception point of       view, if the move constructor throws? The first element was moved from       its original position and is no longer there. As an attempt to stick to       the strong safety guarantee, the vector could try to move the first       element back from the new buffer to where it used to be. However, this       operation might also throw and things get worse. Therefore, in this       case, the vector cannot provide the strong safety guarantee.              What's is happening then? If T has just either a copy or a move       constructor, then vector |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca