Freestanding Library: Easy [utilities], [ranges], and [iterators]

Document number: P1642R3
Date: 2020-05-24
Reply-to: Ben Craig <ben dot craig at gmail dot com>
Audience: Library Evolution Working Group

Change history

R3

R2

R1

R0

Branching from P0829R4. This "omnibus" paper is still the direction I am aiming for. However, it is too difficult to review. It needs to change with almost every meeting. Therefore, it is getting split up into smaller, more manageable chunks.

Limiting paper to the [utilities], [ranges], and [iterators] clauses.

Introduction

This paper proposes adding many of the facilities in the [utilities], [ranges], and [iterators] clause to the freestanding subset of C++. The paper will only be adding complete entities, and will not tackle partial classes. In other words, classes like pair and tuple are being added, but trickier classes like optional, variant, and bitset will come in another paper.

The <memory> header has a dependency on facilities in <ranges> and <iterator>, so those headers (and clauses) are addressed as well.

Motivation and Design

Many existing facilities in the C++ standard library could be used without trouble in freestanding environments. This series of papers will specify the maximal subset of the C++ standard library that does not require an OS or space overhead.

For a more in depth rationale, see P0829.

<optional>, <variant>, and <bitset> are not in this paper, as all have non-essential functions that can throw an exception. <charconv> is not in this paper as it will require us to deal with the thorny issue of overload sets involving floating point and non-floating point types. I plan on addressing all four of these headers in later papers, so that the topics in question can be debated in relative isolation.

Splitting overload sets

Modifying an overload set needs to be treated with great care. Ideally, libraries built in freestanding environments will have the same semantics (including selected overloads) when built in a hosted environment. I don't think that goal is 100% achievable, as sufficiently clever programmers will be able to detect the difference and modify behavior accordingly.

My approach will be to avoid splitting overload sets that could cause accidental freestanding / hosted differences. In future papers, I may need to lean on = delete techniques to avoid silent behavior changes, but this paper hasn't needed that approach.

swap has shared_ptr, weak_ptr, and unique_ptr overloads, but this paper only marks the unique_ptr overload as freestanding. <memory> has many algorithms with ExecutionPolicy overloads. This paper does not mark the ExectuionPolicy overloads as freestanding. I was unable to come up with any compelling "accidental" way to select one swap or algorithm overload in freestanding, but a different one in hosted.

Also note that the swap overload set visible to a given translation unit is already indeterminate. A user may include <memory> which guarantees the smart pointer overloads, but an implementation could expose any (or none!) of the other swap overloads from other STL headers.

Justification for omissions

The following functions and classes rely on dynamic memory allocation and exceptions:

The following classes rely on iostreams facilities. iostreams facilities use dynamic memory allocations and rely on the operating system.

The ExecutionPolicy overloads of algorithms are of minimal utility on systems that do not support C++ threads.

Feature test macros

Add the following feature test macros to freestanding only:
Name Header
__cpp_lib_freestanding_utility <utility>
__cpp_lib_freestanding_tuple <tuple>
__cpp_lib_freestanding_ratio <ratio>
__cpp_lib_freestanding_memory <memory>
__cpp_lib_freestanding_functional <functional>
__cpp_lib_freestanding_iterator <iterator>
__cpp_lib_freestanding_ranges <ranges>

The following, existing feature test macros cover some features that I am making freestanding, and some features that I am not requiring to be freestanding. These feature test macros won't be required in freestanding, as they could cause substantial confusion when the hosted parts of those features aren't available. The header-based __cpp_lib_freestanding_* macros should provide a suitable replacement in freestanding environments.

Wording

Wording assumes that P1641 has been applied.

Full headers

Instructions to the editor:
Please add a // freestanding comment to the beginning of the following synopses. These headers are entirely freestanding.

Change in [memory.syn]

Instructions to the editor:
Please add a // freestanding comment to the following declarations: Note: the following portions of <memory> are omitted. No change to the working draft should be made for these declarations:

Change in [functional.syn]

Instructions to the editor:
Please add a // freestanding comment to every entity in <functional> except for the following entities:

Change in [iterator.synopsis]

Instructions to the editor:
Please add a // freestanding comment to every entity in <iterator> except for the following entities:

Change in [ranges.syn]

Instructions to the editor:
Please add a // freestanding comment to every entity in <ranges> except for the following entities:

Change in [version.syn]

Please add a // freestanding comment to each of the following macro definitions: Please add the following macro definitions:

#define __cpp_lib_freestanding_functional   202005L // also in <functional>, freestanding only
#define __cpp_lib_freestanding_iterator     202005L // also in <iterator>, freestanding only
#define __cpp_lib_freestanding_memory       202005L // also in <memory>, freestanding only
#define __cpp_lib_freestanding_ranges       202005L // also in <ranges>, freestanding only
#define __cpp_lib_freestanding_ratio        202005L // also in <ratio>, freestanding only
#define __cpp_lib_freestanding_tuple        202005L // also in <tuple>, freestanding only
#define __cpp_lib_freestanding_utility      202005L // also in <utility>, freestanding only

Acknowledgements

Thanks to Brandon Streiff, Joshua Cannon, Phil Hindman, and Irwan Djajadi for reviewing P0829.

Thanks to Odin Holmes for providing feedback and helping publicize P0829.

Thanks to Paul Bendixen for providing feedback while prototyping P0829.

Similar work was done in the C++11 timeframe by Lawrence Crowl and Alberto Ganesh Barbati in N3256.