auto __last = ranges::end(__r);
__glibcxx_assert(__first != __last);
auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
- minmax_result<range_value_t<_Range>> __result = {*__first, *__first};
+ minmax_result<range_value_t<_Range>> __result = {*__first, __result.min};
if (++__first == __last)
return __result;
else
#include <algorithm>
#include <string>
+#include <utility>
#include <vector>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
VERIFY( result.min == "a"s && result.max == "c"s );
}
+struct A {
+ A() = delete;
+ A(int i) : i(i) { }
+ A(const A&) = default;
+ A(A&& other) : A(std::as_const(other)) { ++move_count; }
+ A& operator=(const A&) = default;
+ A& operator=(A&& other) {
+ ++move_count;
+ return *this = std::as_const(other);
+ };
+ friend auto operator<=>(const A&, const A&) = default;
+ static inline int move_count = 0;
+ int i;
+};
+
+void
+test06()
+{
+ // PR libstdc++/104858
+ // Verify ranges::minmax doesn't dereference the iterator for the first
+ // element in the range twice.
+ A a(42);
+ ranges::subrange r = {std::move_iterator(&a), std::move_sentinel(&a + 1)};
+ auto result = ranges::minmax(r);
+ VERIFY( A::move_count == 1 );
+ VERIFY( result.min.i == 42 && result.max.i == 42 );
+}
+
int
main()
{
test03();
test04();
test05();
+ test06();
}