<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/102164>102164</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[libc++] Detect use of unreachable sentinels in `from_range` constructors
</td>
</tr>
<tr>
<th>Labels</th>
<td>
libc++,
ranges
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ldionne
</td>
</tr>
</table>
<pre>
In the following code, we create a vector from a pipeline based on a `iota_view` that returns an `unreachable_sentinel`:
```c++
std::vector<int> vec(std::from_range, std::views::iota(0uz) | std::views::transform(foo));
```
The `vector` constructor happily tries to iterate from begin to end of that range, which obviously doesn't work since that's an unbounded range. I think we could pretty easily do better here. We could potentially introduce an internal trait like `__is_bounded_range` or `__is_finite_range` (or an iterator/sentinel variant thereof) and specialize it for the library-provided views. This would propagate the fact that the transform iterator used here is wrapping something that isn't bounded.
We would then check inside `vector`'s `from_range` constructor (and probably a lot of other places, I guess this applies to all algorithms too) whether we are operating on an unbounded range. The trait would have to represent whether we know that the range is definitely unbounded, because in some cases we might not know for sure.
https://godbolt.org/z/nEMMPaYYo
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8VE9r4zoQ_zTKZdjgyPl78GG33UAPC-9QWPYUxvbEnldFMtI4If30j1HcNi3LA-PElvXT75-EKXHniSqz-mFWjzMcpQ-xci0H72lWh_ZaPXmQnuAYnAsX9h00oSVjH-BC0ERCIUA4UyMhwjGGEyAMPJBjT1BjohaCBwSzLjgIHs5MF7MuQHoUiCRj9AnQ6_joI2HTY-3okMgLe3JmXZjyuykeTfF2Xxe3qzH2h175bZJWvyu_35iY8oG9mPKnMjN2-z6sDA8RfZclfMxiuqTbX2Vp7LYYX43dgdn8_SuJ6NMxxJOx22MIxu70Kn984XhP_LknVTkRXBfQBJ8kjtm5HoeB3RUkMiWQACwU1dtsaU0de31LvoVwnMx7U3Hpuekh1GcOY3JXaAMl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