| Stewbond (1842) | |||
|
The reference on std::swap disclaims that it uses a copy constructor and two assignment operators which may not be the most efficient way to swap two containers. I was thinking about it and came up with this:
It works for primitive types, but to work on classes the ^= operator needs to be overloaded. Is there a way to cast a complex object to raw binary data so that I can still perform bit-wise operations on it? I know this sounds like a REALLY bad idea, but the more I think about it, the better it sounds to me. Example, a class containing an entire 15MB wave file would really just have the data in an array, aka a pointer. Using a bitwise XOR would swap the pointer, which swaps the data reference without the need to physically move that data. Even if that data is in a std::vector, at a very low level it's still all just pointers to data (sizeof doesn't change depending on the operations I perform). The only part that may be funky would be the function/instruction pointers that are internal to the class. I'm not entirely sure how those would transfer over. | |||
|
|
|||
| Cubbi (1924) | ||||||
|
1. std::swap is defined in terms of move constructor and move assignment. 2. the xor swap hack is useless even for primitive types: plain std::swap is simpler and faster
3. For classes, swap() is a fundamental member function, most well-designed C++ classes provide it along with copy/move constructors (see std::vector for example). Before moves became part of the language, they were implemented through swaps. (edit, dropped the directives in the second paste) | ||||||
|
Last edited on
|
||||||
| ne555 (4382) | |
|
Containers have an specialized version that swaps the pointers. You can always obtain a pointer and read it as you please. | |
|
|
|
| Stewbond (1842) | |||||||
|
That's confusing, the only difference between those two codes are the three extra xorl instructions that I assume represent the three ^= that I put in there. I assume the rest is the function call stuff. So I'm confused on what the std::swap is even doing there. Forgive my ignorance of ASM, but could it be hidden in that .cfg_startproc? This is what it says according to my understanding:
But thanks for the replies. I know about the STL swap functions, but I'm still not quite sure why the xor hack won't work. Anyways, it was just an academic question (I don't plan on implementing this in a project). | |||||||
|
Last edited on
|
|||||||
| Cubbi (1924) | |
| sorry, forgot to edit out the cfi_startproc in the second paste, it's not code | |
|
|
|
| EssGeEich (1007) | |||
You should begin with some MemXor function and derive a MemSwap from it:
| |||
|
Last edited on
|
|||
| ne555 (4382) | |
|
> The only part that may be funky would be the function/instruction pointers that are internal to the class ¿what are you talking about? | |
|
|
|
| JLBorges (1752) | |||
|
> a class containing an entire 15MB wave file would really just have the data in an array, aka a pointer. > Using a bitwise XOR would swap the pointer, which swaps the data reference > without the need to physically move that data. Define a noexcept destructor, noexcept move constructor and a noexcept move assignment operator and std::swap() would do the same thing.
> I'm still not quite sure why the xor hack won't work. Assume that it works flawlessly; you would still be just addressing one special case. Supporting move semantics is far more general; with move, std::vector<A> etc. would also be able to eliminate gratuitous copying of objects of type A.
| |||
|
|
|||