Skip to main content

One post tagged with "C++20"

C++20 is a major version of the C++ programming language standard, introducing new features and improvements such as concepts, ranges, coroutines, modules, and enhanced constexpr capabilities. It aims to enhance code clarity, performance, and maintainability while providing modern programming paradigms for developers.

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.