Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MC-DeltaT opened this issue Nov 19, 2020 · 0 comments

Comments

@MC-DeltaT
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant