Add missing <algorithm> headers and fix optional_ptr copy constructor