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.
Loading...
Searching...
No Matches
iterator.h File Reference

Forward declaration and duck-typed iterator concept definitions. More...

#include <concepts>
#include <type_traits>
#include <utility>

Go to the source code of this file.

Namespaces

namespace  jh::concepts
 Behavioral concept namespace of the JH Toolkit.

Concepts

concept  jh::concepts::indirectly_readable
 Concept for types that can be read indirectly via dereference.
concept  jh::concepts::indirectly_writable
 Concept for types that support indirect write operations through dereference.
concept  jh::concepts::is_iterator
 Concept for detecting iterator-like types based on behavior.
concept  jh::concepts::sentinel_for
 Concept for detecting sentinel-iterator compatibility.
concept  jh::concepts::input_iterator
 Concept for readable, comparable single-pass iterators.
concept  jh::concepts::output_iterator
 Concept for writable single-pass iterators.
concept  jh::concepts::forward_iterator
 Concept for multi-pass, readable, and self-sentinel iterators.
concept  jh::concepts::bidirectional_iterator
 Concept for iterators supporting bidirectional traversal.
concept  jh::concepts::random_access_iterator
 Concept for iterators supporting random access operations.

Typedefs

template<typename I>
using jh::concepts::iterator_value_t = typename detail::iterator_value_impl<I>::type
 Deduces the value type of a duck-typed iterator.
template<typename I>
using jh::concepts::iterator_reference_t = typename detail::iterator_reference_impl<I>::type
 Deduces the reference type of a duck-typed iterator.
template<typename I>
using jh::concepts::iterator_rvalue_reference_t = decltype(detail::adl_iter_move(std::declval<I &>()))
 Deduces the rvalue reference type of a duck-typed iterator.
template<typename I>
using jh::concepts::iterator_difference_t = typename detail::iterator_difference_impl<I>::type
 Deduces the difference type of a duck-typed iterator.
template<typename Container>
using jh::concepts::iterator_t = typename detail::iterator_resolver<Container>::type
 Deduces the iterator type associated with a container, pointer, or array.

Detailed Description

Forward declaration and duck-typed iterator concept definitions.

Author
JeongHan-Bae <mastropseudo@gmail.com>

This header defines the unified iterator interface and concept suite for the JH container framework. Unlike traditional STL iterators that rely on static typedefs such as iterator_category or difference_type, this design applies behavioral duck-typing — a type is recognized as an iterator if it behaves like one.

Overview

  • Provides a forward declaration of jh::iterator<> for cross-module compatibility.
  • Defines behavior-based iterator concepts (input_iterator, output_iterator, etc.).
  • Supports STL iterators, pointers, arrays, and user-defined duck-typed iterators.

Design Principles

  1. Behavioral Duck Typing

    Iterators are recognized by behavior — valid expressions like *it, ++it, it++ — not by typedefs. Any type usable in for(auto&& x : container) qualifies, covering both for(auto& x : ...) and for(auto x : ...).

  2. Unified Deduction Model

    iterator_t<Container> resolves iterators by fallback: jh::Container<T>::typeContainer::iteratorContainer.begin()T*raw pointer → T[]array decay.

  3. STL Compatibility

    Works with STL iterators, pointers, arrays, and custom duck-typed types, with no dependency on <iterator> or <ranges>.

  4. Minimal Dependencies

    Built only on <concepts>, <type_traits>, and <utility>. Traits and categories are deduced via valid expressions instead of std::iterator_traits.

  5. Range Bridging

    Reuses the same duck-typing model for jh::concepts::sequence and to_range(), enabling STL-style iteration for any non-destructive range-like type.

Concept Summary

ConceptBehavior CheckedPrimary Use
is_iteratorBasic iteration (*it, ++it)Type detection
indirectly_readableDereference validity and value/reference consistencyReadable element access
indirectly_writable<Out, T>Supports assignment through *it = valueWritable element access
sentinel_for<S, I>Equality/inequality comparability between iterator and sentinelRange boundary detection
input_iteratorReadable, comparable iteration; refines is_iterator and indirectly_readableSingle-pass sequential read
output_iteratorWritable via *it = value; refines is_iterator and indirectly_writableSingle-pass sequential write
forward_iteratorRefines input_iterator; multi-pass, copyable, and self-sentinel (sentinel_for<I, I>)Stable, reentrant traversal
bidirectional_iteratorRefines forward_iterator; supports reverse movement (–it, it–)Reversible traversal
random_access_iteratorRefines bidirectional_iterator; supports arithmetic and indexing (it + n, it[n])Contiguous or offset access
iterator_t<T>Unified iterator deduction via specialization, begin(), pointer, or array decayMeta-type for resolving iterator type

Purpose

These definitions unify STL and custom iterator semantics under a single duck-typed system. A type is recognized as an iterator by behavioral conformance, not by static signature or inheritance.

Version
1.3.x
Date
2025