Move_iterator is broken in that iterators return prvalues โ€‹โ€‹and return a dangling link

I looked at the STL sources for std::move_iterator<Iterator> and found out that it returns Iterator::value_type&& . This leads to incorrect behavior when Iterator::reference is a r value and does not match Iterator::value_type& .

I have a class with a proxy object as reference (e.g. in std::vector<bool> ) which can implicitly use for value_type . A regular iterator simply casts this proxy (this requires input-iterator requirements), but std::move_iterator causes a cast to value_type with overhead and then returns a link to the created temporary object.

std::move_iterator still works with std::vector<bool> for some reason (probably due to the fact that bool is a simple type and dangling bool&& does not cause an error), but not with my class. It bothers me, and I donโ€™t understand how to fix it, I think this is a mistake in the STL.

Here is the simplified part of the source for std::move_iterator from GCC 4.8.1:

 template <typename Iterator> class move_iterator { public: typedef typename iterator_traits<Iterator>::value_type value_type; typedef value_type&& reference; reference operator*() const { return std::move(*it); } private: Iterator it; }; 
+6
source share
1 answer

The problem you are facing is known to the C ++ committee. This issue is tracked by the LWG-2106 release . This issue is currently in the Open state, which means that permission has not yet been accepted.

+7
source

All Articles