Enlico
Enlico

Reputation: 28490

Function description vs possible implementation on C++ reference

In both the reference pages of std::lower_bound and std::upper_bound from C++ reference I read

[...] uses operator< to compare the elements, [...]

This is very useful to me, because with this information I know that, even if the latter function

Returns an iterator pointing to the first element in the range [first, last) that is greater than value

it still uses the operator< to do so, and not the operator>, so the former has to be defined for the class/type of the objects stored in the container. The Possible implementation section, with the line if (!(value < *it)) {, just confirms this.

However, for instance, the reference page for std::remove, for which I read that

Removes all elements that are equal to value

does not mention any oparator at all, so in principle I would not know which one/ones is/are assumed to be defined in the class of the objects stored in the container. The Possible implementation uses operator== (see the line if (!(*i == value))).

Hence my question: is it intentional that the documentation pages of some functions don't specify the "requirements" that the classes on which the function is called must satisfy?

Upvotes: 2

Views: 93

Answers (1)

Miles Budnek
Miles Budnek

Reputation: 30619

While cppreference is generally quite good, it is a community-maintained project; not official documentation. It also sometimes uses slightly ambiguous wording to make the text more understandable.

For the official requirements, we must turn to the standard. There, all of these requirements are explicitly spelled out:

From [lower.bound]

Let comp be less{} and proj be identity{} for overloads with no parameters by those names.
...
Returns: The furthermost iterator i in the range [first, last] such that for every iterator j in the range [first, i), bool(invoke(comp, invoke(proj, *j), value)) is true.

From [upper.bound]

Let comp be less{} and proj be identity{} for overloads with no parameters by those names.
...
Returns: The furthermost iterator i in the range [first, last] such that for every iterator j in the range [first, i), !bool(invoke(comp, invoke(proj, *j), value)) is true.

From [alg.remove]

Let E be
-- bool(*i == value) for remove,
...
Effects: Eliminates all the elements referred to by iterator i in the range [first, last) for which E holds.

There is no ambiguity in these descriptions. std::lower_bound and std::upper_bound use std::less to do their comparisons by default, and std::remove uses operator==.

Upvotes: 3

Related Questions