Skip to main content

One post tagged with "C++23"

C++23 is the latest version of the C++ programming language standard, building upon the features introduced in C++20. It includes further enhancements to the language, library improvements, and new features aimed at improving developer productivity, code safety, and performance.

View All Tags

Extending Ranges and Views to Non-Iterable Container Adaptors

· 14 min read
Austin Kim
Staff Software Engineer, System programming and media framework specialist

1. Motivation

std::stack, std::queue, and std::priority_queue are container adaptors: thin wrappers around an underlying sequence container (vector, deque, list) that expose a deliberately restricted interface — push, pop, top/front, empty, size. None of them expose begin()/end(). This is not an oversight; it is a design decision inherited from the original SGI STL, whose documentation states outright that priority_queue "does not allow iteration through its elements." The rationale is that the adaptor's whole contract is "you may only ever see the current top/front element," and exposing arbitrary iteration would leak the underlying container's storage order (vector's heap layout, in the case of priority_queue), which is not part of the abstraction.

The consequence is that these three types cannot participate in the modern iteration and composition idioms C++ has accumulated since C++11 — range-based for, and since C++20, the <ranges> view/pipe machinery — because both require some notion of begin()/end(). This document describes pop_range, a small header-only library that closes that gap by giving these adaptors a well-defined, honestly-named destructive view: iterating it doesn't just read the adaptor, it drains it, one pop() per increment.