3031. Algorithms and predicates with non-const reference arguments

Section: 28.7 [alg.sorting] Status: New Submitter: Jonathan Wakely Opened: 2017-11-08 Last modified: 2017-11-09

Priority: 2

View all other issues in [alg.sorting].

View all issues with New status.


This doesn't compile with any major implementation:

int i[1] = { };
std::stable_sort(i, i, [](int& x, int& y) { return x < y; });

The problem is that the Compare expects non-const references. We say "It is assumed that comp will not apply any non-constant function through the dereferenced iterator" But that isn't sufficient to forbid the example.

My first thought was to modify [alg.sorting] to make the Compare requirements use comp(as_const(x), as_const(x)) but that would get very verbose to add to every expression using comp.

[2017-11 Albuquerque Wednesday night issues processing]

Priority set to 2; Jonathan to improve the statement of the problem.

Proposed resolution: