
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
59 lines
1.6 KiB
Plaintext
59 lines
1.6 KiB
Plaintext
== Why is this an issue?
|
||
|
||
Using ``++<algorithm>++`` header non-modifying operations ``++all_of++``, ``++none_of++`` and ``++any_of++`` allows to simplify the code and make it less error-prone.
|
||
|
||
|
||
When using versions of the standard between {cpp}11 and {cpp}17 included you should use:
|
||
|
||
* ``++std::all_of++``: return ``++true++`` if all elements in the given range are matching the given predicate, ``++false++`` otherwise
|
||
* ``++std::none_of++``: return ``++true++`` if no elements in the given range are matching the given predicate, ``++false++`` otherwise
|
||
* ``++std::any_of++``: return ``++true++`` if at least one element in the given range is matching the given predicate, ``++false++`` otherwise
|
||
|
||
In {cpp}20, range-based alternatives have been introduced:
|
||
|
||
* ``++std::ranges::all_of++``
|
||
* ``++std::ranges::none_of++``
|
||
* ``++std::ranges::any_of++``
|
||
|
||
This rule will detect common patterns that can be replaced by these three STL algorithms.
|
||
|
||
|
||
=== Noncompliant code example
|
||
|
||
[source,cpp]
|
||
----
|
||
bool asDesired(const int v);
|
||
|
||
bool areAllDesired(std::vector<int> values) {
|
||
for (int val : values) { // Noncompliant, replace it by a call to std::all_of
|
||
if (!asDesired(val)) {
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
----
|
||
|
||
|
||
=== Compliant solution
|
||
|
||
[source,cpp]
|
||
----
|
||
bool asDesired(const int v);
|
||
|
||
bool areAllDesired2(std::vector<int> values) {
|
||
return std::all_of(std::begin(values), std::end(values), asDesired);
|
||
}
|
||
----
|
||
|
||
|
||
ifdef::env-github,rspecator-view[]
|
||
'''
|
||
== Comments And Links
|
||
(visible only on this page)
|
||
|
||
=== on 19 Nov 2020, 14:39:48 Abbas Sabra wrote:
|
||
Implemented as part of S5566
|
||
|
||
endif::env-github,rspecator-view[]
|