]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: Add nodiscard in <algorithm>
authorJonathan Wakely <jwakely@redhat.com>
Mon, 26 Feb 2024 13:17:13 +0000 (13:17 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 14 Mar 2024 16:58:15 +0000 (16:58 +0000)
commitdf483ebd24689a3bebfae2089637a00eca0e5a12
tree36b8dc8090f01637412d73ef2d629e643a064742
parentfd7104388406b77ad4b81eb77d976ecfff848913
libstdc++: Add nodiscard in <algorithm>

Add the [[nodiscard]] attribute to several functions in <algorithm>.
These all have no side effects and are only called for their return
value (e.g. std::count) or produce a result that must not be discarded
for correctness (e.g. std::remove).

I was intending to add the attribute to a number of other functions like
std::copy_if, std::unique_copy, std::set_union, and std::set_difference.
I stopped when I noticed that MSVC doesn't use it on those functions,
which I suspect is because they're often used with an insert iterator
(e.g. std::back_insert_iterator). In that case it doesn't matter if
you discard the result, because you have the container to tell you how
many elements were copied to the output range.

libstdc++-v3/ChangeLog:

* include/bits/stl_algo.h (find_end, all_of, none_of, any_of)
(find_if_not, is_partitioned, partition_point, remove)
(remove_if, unique, lower_bound, upper_bound, equal_range)
(binary_search, includes, is_sorted, is_sorted_until, minmax)
(minmax_element, is_permutation, clamp, find_if, find_first_of)
(adjacent_find, count, count_if, search, search_n, min_element)
(max_element): Add nodiscard attribute.
* include/bits/stl_algobase.h (min, max, lower_bound, equal)
(lexicographical_compare, lexicographical_compare_three_way)
(mismatch): Likewise.
* include/bits/stl_heap.h (is_heap, is_heap_until): Likewise.
* testsuite/25_algorithms/equal/debug/1_neg.cc: Add dg-warning.
* testsuite/25_algorithms/equal/debug/2_neg.cc: Likewise.
* testsuite/25_algorithms/equal/debug/3_neg.cc: Likewise.
* testsuite/25_algorithms/find_first_of/concept_check_1.cc:
Likewise.
* testsuite/25_algorithms/is_permutation/2.cc: Likewise.
* testsuite/25_algorithms/lexicographical_compare/71545.cc:
Likewise.
* testsuite/25_algorithms/lower_bound/33613.cc: Likewise.
* testsuite/25_algorithms/lower_bound/debug/irreflexive.cc:
Likewise.
* testsuite/25_algorithms/lower_bound/debug/partitioned_neg.cc:
Likewise.
* testsuite/25_algorithms/lower_bound/debug/partitioned_pred_neg.cc:
Likewise.
* testsuite/25_algorithms/minmax/3.cc: Likewise.
* testsuite/25_algorithms/search/78346.cc: Likewise.
* testsuite/25_algorithms/search_n/58358.cc: Likewise.
* testsuite/25_algorithms/unique/1.cc: Likewise.
* testsuite/25_algorithms/unique/11480.cc: Likewise.
* testsuite/25_algorithms/upper_bound/33613.cc: Likewise.
* testsuite/25_algorithms/upper_bound/debug/partitioned_neg.cc:
Likewise.
* testsuite/25_algorithms/upper_bound/debug/partitioned_pred_neg.cc:
Likewise.
* testsuite/ext/concept_checks.cc: Likewise.
* testsuite/ext/is_heap/47709.cc: Likewise.
* testsuite/ext/is_sorted/cxx0x.cc: Likewise.
24 files changed:
libstdc++-v3/include/bits/stl_algo.h
libstdc++-v3/include/bits/stl_algobase.h
libstdc++-v3/include/bits/stl_heap.h
libstdc++-v3/testsuite/25_algorithms/equal/debug/1_neg.cc
libstdc++-v3/testsuite/25_algorithms/equal/debug/2_neg.cc
libstdc++-v3/testsuite/25_algorithms/equal/debug/3_neg.cc
libstdc++-v3/testsuite/25_algorithms/find_first_of/concept_check_1.cc
libstdc++-v3/testsuite/25_algorithms/is_permutation/2.cc
libstdc++-v3/testsuite/25_algorithms/lexicographical_compare/71545.cc
libstdc++-v3/testsuite/25_algorithms/lower_bound/33613.cc
libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/irreflexive.cc
libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/partitioned_neg.cc
libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/partitioned_pred_neg.cc
libstdc++-v3/testsuite/25_algorithms/minmax/3.cc
libstdc++-v3/testsuite/25_algorithms/search/78346.cc
libstdc++-v3/testsuite/25_algorithms/search_n/58358.cc
libstdc++-v3/testsuite/25_algorithms/unique/1.cc
libstdc++-v3/testsuite/25_algorithms/unique/11480.cc
libstdc++-v3/testsuite/25_algorithms/upper_bound/33613.cc
libstdc++-v3/testsuite/25_algorithms/upper_bound/debug/partitioned_neg.cc
libstdc++-v3/testsuite/25_algorithms/upper_bound/debug/partitioned_pred_neg.cc
libstdc++-v3/testsuite/ext/concept_checks.cc
libstdc++-v3/testsuite/ext/is_heap/47709.cc
libstdc++-v3/testsuite/ext/is_sorted/cxx0x.cc