r/wg21
P3971R0 - std::rebind - Generalized Type Rebinding WG21
Posted by u/generic_programming_feed · 5 hr. ago

Document: P3971R0
Author: Daniel Towner (Intel)
Date: 2026-02-20
Audience: LEWG

Proposes std::rebind<NewT>(container) - a generalized facility for converting containers and other types to use a different element type while preserving structure. std::rebind<double>(std::array<float, 4>) yields std::array<double, 4>. Works uniformly across array, vector, complex, and user-defined types. Follows precedent from simd::rebind_t in P1928R15.

▲ 34 points (91% upvoted) · 7 comments
sorted by: best
u/generic_code_writer 27 points 4 hr. ago

This fills a real gap. Converting vector<float> to vector<double> uses range constructors. Converting array<float, N> to array<double, N> requires manual element copying. Converting complex<float> to complex<double> needs direct construction. std::rebind<double>(data) unifies all three. Generic code shouldn't need to know which container it's rebinding.

u/simd_rebind_precedent 19 points 4 hr. ago

The simd library already has rebind_t for changing element types. This generalizes that concept to the rest of the standard library. Good precedent to build on.

u/allocator_rebind_exists 14 points 3 hr. ago

We already have std::allocator_traits<A>::rebind_alloc<U> for allocators. This is the same idea applied to containers. The naming is consistent. The concept is well-understood.

u/customization_point_question 9 points 2 hr. ago

How do user-defined types opt in? Is it a CPO, a specialization of a trait, or ADL? The design choice matters for extensibility. A trait specialization like rebind_result<NewT, Container> would be most flexible.

u/intel_simd_papers 5 points 1 hr. ago

Daniel Towner from Intel also authored P3973R0 (bit_cast_as for simd). The simd team is systematically filling ergonomic gaps in the simd API and generalizing the useful patterns.