Document number: P0858R0
Project: Programming Language C++
Audience: Library Evolution Working Group, Library Working Group
 
Antony Polukhin <antoshkka@gmail.com>, <antoshkka@yandex-team.ru>
 
Date: 2017-11-11

Constexpr iterator requirements

I. Introduction and Motivation

This proposal resolves issues LWG 2897 and LWG 2938, which intend to make the iterators of some classes usable in constant expressions. We expect more constexpr usable containers soon from reflection proposals and from work that goes on for an accepted P0639R0: Changing attack vector of the constexpr_vector proposal.

This proposal follows the LWG recommendation and attempts to solve both issues in a generic way by introducing the constexpr iterator requirement.

Such requirement will easily allow to make constexpr usable iterators by only adding a few words to the iterator requirements of a container.

Note that swap is excluded from constexpr iterator requirement until CWG 1581 is resolved. Without that resolution making swap constexpr may break user code.

II. Impact on the Standard

This proposal is a pure library extension. It proposes changes that do not break existing code and do not degrade performance. It does not require any changes in the core language.

III. Proposed wording relative to the Working Draft N4687.

Add a new paragraph at the end of 27.2.1 [iterator.requirements.general] as indicated:

-12- An invalid iterator is an iterator that may be singular.(footnote: […])

Iterators are called constexpr iterators if all operations provided to satisfy iterator category operations are constexpr functions, except for

[Note: For example, the types "pointer to int" and reverse_iterator<int*> are constexpr iterators. – end note]

-13- In the following sections, a and b denote […]

Add a new paragraph at the end of 26.3.7.1 [array.overview]:

-3- An array satisfies all of the requirements of a container […]

-?- iterator and const_iterator satisfy the constexpr iterator requirements (27.2.1 [iterator.requirements.general]).

Add to the end of the 24.4.2 [string.view.template] section:

-1- In every specialization basic_string_view<charT, traits>, the type traits shall satisfy the character traits requirements (21.2), and the type traits::char_type shall name the same type as charT.

-?- iterator satisfies the constexpr iterator requirements (27.2.1 [iterator.requirements.general]).

IV. Feature-testing macros

For the purposes of SG10, we recommend updating the values of the feature-testing macros __cpp_lib_string_view and __cpp_lib_array_constexpr to the date of adoption (expected: 201803).

V. References

[N4687] Working Draft, Standard for Programming Language C++. Available online at www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4687.pdf.