I'm trying to implement a container which (for some reason) does not allow to implement a simple "reference type". It is something similar to vector<bool>, where the reference is actually a small object representing a "reference" to a particular element of the container.
Now I would like to use "modifying" STL algorithms like std::sort for this container and make it as efficient as possible. Everything works quite fine as soon as the algorithm is based simply on swaps and comparisons of the reference objects since both can be done fast even for heavy objects stored in the container.
The problem comes when the algorithm is trying to make a copy to a temporary value like, e.g., in some implementations of the insertion sort where at some point the code contains the statement
value_type val = * iterator;
where * iterator (the custom operator*()) returns a light-weight reference object by value. I could implement a move constructor (as well as the assignment) from reference r-value to the value type and implement it efficiently, but this would effectively "steal" the content of the value the iterator "points" to which is not at all the intention of the above statement. What would be nice if there was a way how to distinguish between this kind of statement and
value_type val = std::move(* iterator)
where I could steal the content of the "reference" without remorse.
I thought about adding a "flag" to the reference object to denote whether or not it is safe to make a move or make a copy instead from the given reference r-value and allow the move as soon as the std::move is applied explicitly. I understand that std::move is simply a static cast so that implementing a "custom" move is not a real option here (like, e.g., having a custom swap).
Any ideas or pointers would be helpful.
Aucun commentaire:
Enregistrer un commentaire