I use STL associative containers ( std::setand std::map) with keys that contain an instance std::unique_ptr<>. A key definition is equivalent to the following:
struct Key
{
std::unique_ptr<Object> object;
bool operator== (const Key& rhs) const { return object->equal (*rhs.object); }
bool operator< (const Key& rhs) const { return object->less (*rhs.object); }
}
Associative STL containers (for example, with C ++ 11) are not known to have a way to get a non-constant key reference for a move. And my keys cannot be copied, so C ++: removing an item from the container and returning it does not work.
Is there a non-UB way to solve this problem?
My current solution is as follows:
template <typename T>
using map_pair_type = std::pair<typename T::key_type, typename T::mapped_type>;
template <typename T>
typename T::value_type take_set (T& container, typename T::iterator iterator)
{
typename T::value_type result = std::move (const_cast<typename T::value_type&> (*iterator));
container.erase (iterator);
return result;
}
template <typename T>
map_pair_type<T> take_map (T& container, typename T::iterator iterator)
{
map_pair_type<T> result {
std::move (const_cast<typename T::key_type&> (iterator->first)),
std::move (iterator->second)
};
container.erase (iterator);
return result;
}
source
share