News for hackers. Stuff that matters.
Re:Clang emits memcpy for std::swap, which can introduce und... by hendzen on Sunday April 24, 2016 @03:40PM (#11561017)
Specifically in the case of a self-swap, which is a somewhat odd thing to do in the first place.
Re:Clang emits memcpy for std::swap, which can introduce und... by dkopi on Sunday April 24, 2016 @04:22PM (#11561204)
Imagine reversing a string:

  while(pStart <= pEnd)
       swap(*pStart++, *pEnd--);
If swap doesn't work for a self swap, this code would break in the case of an odd length string. It's an easy fix in this case, but being able to swap an element with itself is convenient.
Re:Clang emits memcpy for std::swap, which can introduce und... by Matheus28 on Sunday April 24, 2016 @04:55PM (#11561338)
A more common case, would be swapping a vector element with the back of the vector and popping it off, which is kinda common.
Re:Clang emits memcpy for std::swap, which can introduce und... by plorkyeran on Sunday April 24, 2016 @05:54PM (#11561538)
It was common pre-C++11, but these days you should just be doing a move and pop rather than a swap and pop.
Re:Clang emits memcpy for std::swap, which can introduce und... by adrianN on Monday April 25, 2016 @07:47AM (#11563620)
Luckily all existing code has been modernized to C++11 /s.
Re:Clang emits memcpy for std::swap, which can introduce und... by AnthonyMouse on Sunday April 24, 2016 @04:01PM (#11561104)
It's quite common actually. Usually in the context of something like "swap element 'n' with the last element in this vector" without any check to see if 'n' is already the last element, because checking for that condition just to avoid a harmless no-op in the uncommon case is ugly and slower in the common case.
Re:Clang emits memcpy for std::swap, which can introduce und... by boulos on Sunday April 24, 2016 @03:55PM (#11561083)
Before shrink to fit, you could easily have produced this with a few references and a "use std::swap unconditionally".

The std::swap thing is only one pitfall, the reliance on "our memcpy will do the right thing with any overlap" is a bit risky (and doesn't hold with LD_PRELOAD, but I think it may have been switched to using the LLVM based librt project anyway, that is I'm not sure these calls are supposed to become naked memcpy).