iterator icon indicating copy to clipboard operation
iterator copied to clipboard

adaptor_facade_base::operator[] returns value type with iterator of const values

Open MC-DeltaT opened this issue 5 years ago • 0 comments

iterator_facade_base::operator[] returns the iterator's value type if the iterated values are const and POD, as determined by boost::iterators::detail::operator_brackets_result. This results in subtle object lifetime errors in places where a reference is expected.

For example, if you use such an iterator with iterator_range, iterator_range::operator[] becomes UB, as it attempts to return the reference type:

std::vector<double> data{1.0, 3.14, 42.7, 2.618, 9.8};
std::vector<std::size_t> indices{1, 0, 2, 1, 3, 2, 4, 3};

auto const begin = boost::make_permutation_iterator(data.cbegin(), indices.cbegin());
auto const end = boost::make_permutation_iterator(data.cbegin(), indices.cend());

auto const range = boost::make_iterator_range(begin, end);
auto const& e = range[0];   // Returns reference to temporary, UB

Is there any reason why in this case iterator_facade_base::operator[] cannot return the reference type, or even a proxy object, instead?

MC-DeltaT avatar Nov 19 '20 03:11 MC-DeltaT