I’ve started using Position instances to keep track of positions in text, and I found some curious behavior (at least, to me).

Position::operator= isn’t safe for swapping, as it doesn’t actually assign whether the position is maintained. The target lvalue keeps its positionMaintained status before the assignment, and reapplies it after setting all other member vars. This also means that you can’t safely have a std::vector of Positions, or anything where operator= would be used for true copying.

My workaround is just to subclass it with my own operator=, but I’d like to hear your thoughts on this.


Yes, that’s the correct behaviour. Imagine this:

[code]class Thing
masterPosition.setMaintained (true); // always want this value to be maintained!

void setNewMasterPosition (const Position& newPos)
    masterPosition = newPos;  // newPos may or may not be maintained, and we certainly don't want to copy that property across at this point!

Position masterPosition;