Question Details

No question body available.

Tags

c++ c++20 std-ranges

Answers (1)

Accepted Answer Available
Accepted Answer
April 20, 2026 Score: 12 Rep: 47,639 Quality: Expert Completeness: 70%

The question is whether this code triggers undefined behavior?

No. This is well-defined behavior.

When the pr value vector is applied to the range adaptor, it is implicitly converted into an owningview before being applied, and its data ownership is transferred to the owningview.

Therefore, the vw itself now holds the data and is not dangling.

It's important to note that the owning
view is a move-only view, so you must move it to transfer data ownership again:

auto vw = get_vec() | std::views::take(3);

auto vw2 = vw | std::views::take(5); // not ok auto vw2 = std::move(vw) | std::views::take(5); // this is ok