Title
elements_view should not be allowed to return dangling references
Status
new
Section
[range.elements.iterator]
Submitter
Tim Song

Created on 2020-11-18.00:00:00 last changed 1 month ago

Messages

Date: 2020-11-15.00:00:00

[ 2020-11-29; Reflector prioritization ]

Set priority to 2 during reflector discussions.

Date: 2020-11-21.16:11:44

This compiles but the resulting view is full of dangling references:

std::vector<int> vec = {42};
auto r = vec | std::views::transform([](auto c) { return std::make_tuple(c, c); })
             | std::views::keys;

This is because elements_view::iterator::operator* is specified as

constexpr decltype(auto) operator*() const { return get<N>(*current_); }

Here *current_ is a prvalue, and so the get<N> produces a reference into the materialized temporary that becomes dangling as soon as operator* returns.

We should either ban this case altogether, or make operator* (and operator[]) return by value when *current_ is a prvalue and the corresponding tuple element is not a reference (since this get is std::get, we need not worry about weird user-defined overloads.)

History
Date User Action Args
2020-11-29 14:06:26adminsetmessages: + msg11637
2020-11-18 00:00:00admincreate