Skip to content

Implement LWG-3797 elements_view insufficiently constrained#6310

Open
frederick-vs-ja wants to merge 1 commit into
microsoft:mainfrom
frederick-vs-ja:lwg-3797
Open

Implement LWG-3797 elements_view insufficiently constrained#6310
frederick-vs-ja wants to merge 1 commit into
microsoft:mainfrom
frederick-vs-ja:lwg-3797

Conversation

@frederick-vs-ja

@frederick-vs-ja frederick-vs-ja commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Fixes #6204.

However, it seems that elements_view becomes overconstrained with this fix, although it was already overconstrained in C++20. Currently (after LWG-3797) the following code is rejected, while it seems to me that it can be made to work.

using MoveOnlySubrange = std::ranges::subrange<MoveOnlyIter, Sentinel>;
std::vector<MoveOnlySubrange> v;
for (auto&& elem : v | std::views::as_rvalue | std::views::elements<0>) {
    // ...
}

Seems like that the constraints of elements_view are somehow imprecise. Especially, _Has_tuple_element doesn't seem to handle rvalues well. IMO, ideally, they should only match validness of iterator's operator* while additionally requiring tuple-like-ness.

@frederick-vs-ja frederick-vs-ja requested a review from a team as a code owner June 16, 2026 12:48
Copilot AI review requested due to automatic review settings June 16, 2026 12:48
@github-project-automation github-project-automation Bot moved this to Initial Review in STL Code Reviews Jun 16, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR implements LWG-3797 "elements_view insufficiently constrained" by adding a missing constraint to the _Has_tuple_element concept in C++23 mode, ensuring that get<N>(t) is convertible to const tuple_element_t<N, T>&. The pre-C++23 path already had this constraint, but it was inadvertently dropped when the C++23 _Tuple_like concept was introduced.

Changes:

  • Added the get<_Index>(__t) -> convertible_to<const tuple_element_t<_Index, _Tuple>&> constraint to the C++23 branch of _Has_tuple_element.
  • Added compile-only tests verifying that elements_view correctly rejects ranges whose element type has a move-only component (e.g., subranges with move-only input iterators), covering direct vectors, transformed views, as_const, and as_rvalue.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
stl/inc/ranges Adds the missing requires constraint for get convertibility to _Has_tuple_element in C++23 mode
tests/std/tests/P0896R4_views_elements/test.cpp Adds compile-time tests for LWG-3797 using move-only subranges and various view adaptors

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@StephanTLavavej StephanTLavavej added LWG Library Working Group issue ranges C++20/23 ranges labels Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LWG Library Working Group issue ranges C++20/23 ranges

Projects

Status: Initial Review

Development

Successfully merging this pull request may close these issues.

LWG-3797 elements_view insufficiently constrained

3 participants