From 56c999860bbbb2fd5091ba0985e2e5eaa90c6478 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Wed, 20 Jul 2022 16:51:44 +0100 Subject: libstdc++: Fix std::common_iterator assignment [PR100823] This fixes the following conformance problems reported in the PR: - Move constructor and move assignment should be defined. - Copy assignment from a valueless object should be allowed. Assignment is completely rewritten by this patch, as the previous version had a number of problems. The converting assignment failed to handle the case of assigning a new value to a valueless object, which should work. It only accepted lvalue arguments, so wasn't usable to implement the move assignment operator. Finally, it enforced the precondition that the argument is not valueless, which is correct for the converting assignment but not for the copy assignment. A new _M_assign member is added to handle all cases of assignment (copying from an lvalue, moving from an rvalue, and converting from a different type). The not valueless precondition is checked in the converting assignment before calling _M_assign, so isn't enforced for copy and move assignment. The new function no longer uses a switch, so handles valueless objects as the LHS or RHS of the assignment. libstdc++-v3/ChangeLog: PR libstdc++/100823 * include/bits/stl_iterator.h (common_iterator): Define move constructor and move assignment operator. (common_iterator::_M_assign): New function implementing assignment. (common_iterator::operator=): Use _M_assign. (common_iterator::_S_valueless): New constant. * testsuite/24_iterators/common_iterator/100823.cc: New test. --- .../24_iterators/common_iterator/100823.cc | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 libstdc++-v3/testsuite/24_iterators/common_iterator/100823.cc (limited to 'libstdc++-v3/testsuite') diff --git a/libstdc++-v3/testsuite/24_iterators/common_iterator/100823.cc b/libstdc++-v3/testsuite/24_iterators/common_iterator/100823.cc new file mode 100644 index 00000000000..4f2b23de8cc --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/common_iterator/100823.cc @@ -0,0 +1,43 @@ +// { dg-options "-std=gnu++20 -D_GLIBCXX_ASSERTIONS" } +// { dg-do run { target c++20 } } +#include +#include +#include + +void +test_valueless_assignment() +{ + int x[1] { }; + __gnu_test::test_forward_range r(x); + using Iter = decltype(r.begin()); + using Sent = decltype(r.end()); + + std::common_iterator i; + const std::common_iterator j(r.begin()); + try + { + struct Bomb + { + bool operator==(Iter) const { return true; } + operator Sent() const { throw 1; } + }; + std::common_iterator b{Bomb{}}; + i = b; // Throws, leaving i valueless-by-exception. + VERIFY(false); + } + catch (int) + { + std::common_iterator k(i); + + // PR libstdc++/100823 + k = i; // Valid even though both operands are valueless. + + i = j; // No longer valueless. + } + VERIFY( i == j ); +} + +int main() +{ + test_valueless_assignment(); +} -- cgit v1.2.3