Skip to content

Tuple protocol for fixed-size span (P3786R2)#7

Open
SamareshSingh wants to merge 4 commits intomainfrom
tuple-protocol-p3786r2
Open

Tuple protocol for fixed-size span (P3786R2)#7
SamareshSingh wants to merge 4 commits intomainfrom
tuple-protocol-p3786r2

Conversation

@SamareshSingh
Copy link
Copy Markdown
Collaborator

Implements P3786R2 so fixed-size spans support the tuple protocol and structured bindings.

Three additions, per the paper:

  • std::tuple_size<span<T, N>> specialised to integral_constant<size_t, N>.
  • std::tuple_element<I, span<T, N>> with using type = T& (a reference, since a span element lives outside the span).
  • get<I>(span<T, N>) returning T&. Lives in beman::span so ADL picks it up for auto& [a, b, c] = s;.

All three are gated on N != dynamic_extent via constrained partial specialisation. For span<T> (dynamic) the specialisations don't apply, so std::tuple_size<span<T>> falls back to the undefined primary template — that's the SFINAE-friendly behaviour the tuple-like concept relies on.

Top-level cv on the span itself (const span<T, N>) is handled by the existing std::tuple_size<const T> / std::tuple_element<I, const T> partial specialisations in the standard, so we don't need to do anything for that case.

Six new tests:

  • tuple_size_v for fixed extents including 0
  • top-level-const passthrough
  • tuple_element_t returning T& / const T& / int& for const span<int, N>
  • get<I> returning a reference, with mutation visible through the underlying array
  • structured binding binding by reference
  • dynamic-extent span is not tuple-like (detection-idiom check)

Local: 51/51 passing.

Fixes #4

Adds the three pieces from P3786R2:

- std::tuple_size<span<T, N>> - integral_constant<size_t, N>
- std::tuple_element<I, span<T, N>> - using type = T&
- get<I>(span<T, N>) returning T&, in beman::span so ADL finds it for
  structured bindings

All three are gated on N != dynamic_extent via constrained partial specialization, so std::tuple_size<span<T>> falls back to the undefined primary template and remains SFINAE-friendly for the tuple-like concept.

Top-level cv on the span is handled automatically by the existing std::tuple_size<const T> / std::tuple_element<I, const T> partial specs.

Six new tests cover tuple_size value, tuple_element returning references (including const-element-type and top-level-const cases), get returning
a reference to the underlying element, structured bindings binding by reference, and the dynamic_extent SFINAE behaviour.

Fixes #4
ssam18 added 3 commits May 1, 2026 08:41
# Conflicts:
#	include/beman/span/span.hpp
Pure formatter output. Reflows the new tuple_size / tuple_element /
get specializations and tightens up the SpanTuple tests under the
.clang-format AlignConsecutiveDeclarations and ColumnLimit rules.
Also picks up the at() test alignment carried in via the merge.
# Conflicts:
#	tests/beman/span/span.test.cpp
@coveralls
Copy link
Copy Markdown

Coverage Status

coverage: 100.0%. remained the same — tuple-protocol-p3786r2 into main

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

Successfully merging this pull request may close these issues.

implement c++29 feature tuple protocol for fixed size spans

3 participants