Overview
The range iteration API provides a lightweight, zero-allocation facility for iterating over integer ranges with support for:- Forward and backward iteration
- Inclusive and exclusive bounds
- Custom step values
- Strong type compatibility (
offset_t,rva_t, etc.) - Compile-time evaluation (
constexpr) - Sentinel-based iteration
Enumerations
range_dir
Specifies the direction of iteration.
Iterate from lower to higher values (increment)
Iterate from higher to lower values (decrement)
range_mode
Specifies boundary inclusion policy.
The end value is included in the iteration range
The end value is excluded from the iteration range (default)
Functions
range() - Single Parameter
Creates a range from 0 to the specified value.
End value of the range
Direction of iteration (
Forward or Backward)Whether to include the end value in iteration
An iterable range object compatible with range-based for loops
range() - Full Parameters
Creates a range with full control over start, end, and step.
Start value of the range
End value of the range
Step increment/decrement value (must be positive)
Direction of iteration
Boundary inclusion policy
range() - Simplified Overload
Creates a range with default step of 1.
irange() - Inclusive Variants
Convenience wrappers that default to inclusive mode.
irange functions are equivalent to calling range with range_mode::Inclusive.Usage Examples
Forward Exclusive (0 to 9)
Forward Inclusive (0 to 10)
Custom Range (5 to 15)
Backward Iteration (10 to 1)
Custom Step (0, 4, 8, 12, 16)
Strong Type Iteration
Inclusive Range with Step
Termination Logic
Forward Direction
| Mode | Stop Condition |
|---|---|
| Exclusive | current >= end |
| Inclusive | current > end |
Backward Direction
| Mode | Stop Condition |
|---|---|
| Exclusive | current <= end |
| Inclusive | current < end |
Strong Type Support
The range API seamlessly integrates with STX strong types:When using strong types, internal arithmetic operates on the underlying integral type, but the iterator dereference returns the proper strong type, preserving domain separation.
Design Characteristics
- C++23 constexpr-friendly: Fully usable in compile-time contexts
- Zero allocation: No dynamic memory allocation
- Sentinel-based: Efficient iteration termination
- Type safe: Works with strong types without implicit conversion
- Direction-aware: Explicit control over iteration order
- Header-only: No compilation or linking required
- Zero overhead: Compiles to the same machine code as manual loops
Concepts
rangeable
A type is rangeable if it satisfies one of:
- It is an integral type (
int,u32,size_t, etc.) - It is a strong type with:
- A
value_typemember type - A
.get()method returning an integral
- A
Intended Use Cases
- Iterating memory offsets
- Traversing RVA ranges in binary analysis
- Generating aligned index sequences
- Walking binary structures
- Systems-level loop constructs
- Replacing error-prone manual integer loops
Related APIs
strong_typefor domain-safe type wrappersoffset_t,rva_tfor memory address types