|
JH-Toolkit v1.4.1
An engineering-oriented C++20 toolkit with duck-typed concepts, static design, async coroutines, and semantic containers — header-only, RTTI-free, and concurrency-friendly.
|
Container adaptation adaptor — constructs a target container C directly from a compatible range R.
More...
Go to the source code of this file.
Classes | |
| struct | jh::ranges::to_fn< C > |
Function object implementing jh::ranges::to. More... | |
Namespaces | |
| namespace | jh::ranges |
| Semantic pipeline namespace for JH range operations. | |
Functions | |
| template<typename C, std::ranges::range R, typename... Args> | |
| constexpr auto | jh::ranges::to_adaptor (R &&r, Args &&... args) |
Core implementation for jh::ranges::to adaptor. | |
Variables | |
| template<typename C> | |
| constexpr to_fn< C > | jh::ranges::to {} |
Global instance of the to adaptor. | |
Container adaptation adaptor — constructs a target container C directly from a compatible range R.
The jh::ranges::to adaptor provides a high-efficiency way to directly construct a container C from a range R, as long as they form a valid closable_container_for pair.
Conceptually, this corresponds to the closable half of the proposed C++23 std::ranges::to. Unlike jh::ranges::collect, which can always materialize a range via insertion or emplace iteration, to requires that the container and range are structurally compatible and can be constructed directly.
C can be directly constructed from [begin(r), end(r)], the adaptor uses that constructor. C supports move iterators, vector bridging, or underlying container adaptation, these strategies are automatically detected and applied. Args.... C and R form a valid closable_container_for relation. std::vector bridges if needed. collect + toUnder optimized compilation (C++20 and later), the combination collect + to behaves as a semantic two-stage pipeline but is compiled down to a near-optimal one-stage construction.
Although collect materializes an intermediate container (usually std::vector<T>), the compiler's RVO and move-propagation rules ensure that no redundant copies occur for right-value ranges.
| Stage | Responsibility | Typical Behavior | Optimization Result |
|---|---|---|---|
| collect<V>() | Terminates lazy views and produces a normalized, value-semantic container (e.g. std::vector<pair<K,V>>). | Constructs a temporary container on the caller's stack. | RVO creates the container directly in the caller frame; no copy or move is required. |
| to<C>() | Adapts the collected data into the final container C via its constructor ([begin, end), move-iterators, or adapter dispatch). | Passes iterators of the temporary container. | Because the source is a right-value, the target container moves each element; no deep copy occurs. |
| Overall pipeline | Two semantic stages: materialization + construction. | Intermediate vector exists logically but not physically duplicated. | Equivalent runtime cost to C(std::make_move_iterator(...)); no extra allocations, deterministic lifetime. |
to, preventing accidental cross-allocator moves. std::ranges::to. return C(begin(r), end(r)) is specified as a copy operation, compilers perform per-element move for right-value containers, achieving optimal performance without losing semantic clarity. In practice, collect + to achieves the same performance as a monolithic std::ranges::to call while providing stronger guarantees of safety, clarity, and composability within a range pipeline.
1.3.x
2025