This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of C++17 status.
const requirements for associative containersSection: 23.2.7 [associative.reqmts] Status: C++17 Submitter: Daniel Krügler Opened: 2015-09-26 Last modified: 2017-07-30
Priority: 1
View other active issues in [associative.reqmts].
View all other issues in [associative.reqmts].
View all issues with C++17 status.
Discussion:
Table 101 — "Associative container requirements" and its associated legend paragraph
23.2.7 [associative.reqmts] p8 omits to impose constraints related to const
values, contrary to unordered containers as specified by Table 102 and its associated legend
paragraph p11.
key_comp(), value_comp(),
or count() — as non-const functions. In Table 102, "possibly const"
values are exposed by different symbols, so the situation for unordered containers is clear
that these functions may be invoked by const container objects.
For the above mentioned member functions this problem is only minor, because the synopses of the
actual Standard associative containers do declare these members as const functions, but
nonetheless a wording fix is recommended to clean up the specification asymmetry between associative
containers and unordered containers.
The consequences of the ignorance of const becomes much worse when we consider a code
example such as the following one from a recent
libstdc++ bug report:
#include <set>
struct compare
{
bool operator()(int a, int b) // Notice the non-const member declaration!
{
return a < b;
}
};
int main() {
const std::set<int, compare> s;
s.find(0);
}
The current wording in 23.2.7 [associative.reqmts] can be read to require this code to be well-formed,
because there is no requirement that an object comp of the ordering relation of type Compare
might be a const value when the function call expression comp(k1, k2) is evaluated.
const comparison
function objects of associative containers need to support the predicate call expression. This becomes
more obvious when considering the member value_compare of std::map which provides (only) a
const operator() overload which invokes the call expression of data member comp.
[2016-02-20, Daniel comments and extends suggested wording]
It has been pointed out to me, that the suggested wording is a potentially breaking change and should therefore be mentioned in Annex C.
First, let me emphasize that this potentially breaking change is solely caused by the wording change in 23.2.7 [associative.reqmts] p8:[…] and
cdenotes a possiblyconstvalue of typeX::key_compare; […]
So, even if that proposal would be rejected, the rest of the suggested changes could (and should) be
considered for further evaluation, because the remaining parts do just repair an obvious mismatch between
the concrete associative containers (std::set, std::map, …) and the requirement
tables.
const operator(). If the committee really intends to require
a Library to support comparison functors with non-const operator(), this should be clarified by at least
an additional note to e.g. 23.2.7 [associative.reqmts] p8.
[2016-03, Jacksonville]
Move to Ready with Daniel's updated wordingProposed resolution:
This wording is relative to N4567.
Change 23.2.7 [associative.reqmts] p8 as indicated:
-8- In Table 101,
Xdenotes an associative container class,adenotes a value of typeX,bdenotes a possiblyconstvalue of typeX,udenotes the name of a variable being declared,a_uniqdenotes a value of typeXwhenXsupports unique keys,a_eqdenotes a value of typeXwhenXsupports multiple keys,a_trandenotes a possiblyconstvalue of typeXwhen the qualified-idX::key_compare::is_transparentis valid and denotes a type (14.8.2),iandjsatisfy input iterator requirements and refer to elements implicitly convertible tovalue_type,[i, j)denotes a valid range,pdenotes a valid const iterator toa,qdenotes a valid dereferenceable const iterator toa,rdenotes a valid dereferenceable iterator toa,[q1, q2)denotes a valid range of const iterators ina,ildesignates an object of typeinitializer_list<value_type>,tdenotes a value of typeX::value_type,kdenotes a value of typeX::key_typeandcdenotes a possiblyconstvalue of typeX::key_compare;klis a value such thatais partitioned (25.4) with respect toc(r, kl), withrthe key value ofeandeina;kuis a value such thatais partitioned with respect to!c(ku, r);keis a value such thatais partitioned with respect toc(r, ke)and!c(ke, r), withc(r, ke)implying!c(ke, r).Adenotes the storage allocator used byX, if any, orstd::allocator<X::value_type>otherwise, andmdenotes an allocator of a type convertible toA.
Change 23.2.7 [associative.reqmts], Table 101 — "Associative container requirements" as indicated:
[Editorial note: This issue doesn't touch the note column entries for the expressions related tokey_comp()
and value_comp() (except for the symbolic correction), since these are already handled by LWG issues
2227(i) and 2215(i) — end editorial note]
Table 101 — Associative container requirements (in addition to container) (continued) Expression Return type Assertion/note pre-/post-condition Complexity …ab.key_comp()X::key_comparereturns the comparison object
out of whichwasab
constructed.constant ab.value_comp()X::value_comparereturns an object of
value_compareconstructed
out of the comparison objectconstant …ab.find(k)iterator;
const_iteratorfor constant.abreturns an iterator pointing to
an element with the key
equivalent tok, orifab.end()
such an element is not foundlogarithmic …ab.count(k)size_typereturns the number of elements
with key equivalent toklog(ab.size()) +ab.count(k)…ab.lower_bound(k)iterator;
const_iteratorfor constant.abreturns an iterator pointing to
the first element with key not
less thank, orif suchab.end()
an element is not found.logarithmic …ab.upper_bound(k)iterator;
const_iteratorfor constant.abreturns an iterator pointing to
the first element with key
greater thank, orifab.end()
such an element is not found.logarithmic …ab.equal_range(k)pair<iterator, iterator>;
pair<const_iterator, const_iterator>for
constantab.equivalent to make_-.
pair(ab.lower_bound(k),
ab.upper_bound(k))logarithmic …
Add a new entry to Annex C, C.4 [diff.cpp14], as indicated:
C.4.4 Clause 23: containers library [diff.cpp14.containers]
23.2.4 Change: Requirements change: Rationale: Increase portability, clarification of associative container requirements. Effect on original feature: Valid 2014 code that attempts to use associative containers having a comparison object with non-constfunction call operator may fail to compile:#include <set> struct compare { bool operator()(int a, int b) { return a < b; } }; int main() { const std::set<int, compare> s; s.find(0); }