Skip to content

Commit 92860b2

Browse files
author
LoS
committed
[libc++] [test] Further improve proxy Reference class in the test against proxy iterator's lifetime bugs.
1 parent 906eeed commit 92860b2

File tree

1 file changed

+11
-36
lines changed

1 file changed

+11
-36
lines changed

libcxx/test/std/algorithms/robust_against_proxy_iterators_lifetime_bugs.pass.cpp

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -143,24 +143,11 @@ class LifetimeIterator {
143143
lifetime_cache.insert(this);
144144
}
145145

146-
Reference& operator=(const Reference& rhs) {
146+
Reference operator=(const Reference& rhs) const {
147147
assert(lifetime_cache.contains(this) && lifetime_cache.contains(&rhs));
148+
assert(!moved_from_);
148149
assert(!rhs.moved_from_);
149-
150-
*v_ = *rhs.v_;
151-
moved_from_ = false;
152-
153-
return *this;
154-
}
155-
156-
Reference& operator=(Reference&& rhs) noexcept {
157-
assert(lifetime_cache.contains(this) && lifetime_cache.contains(&rhs));
158-
159-
assert(!rhs.moved_from_);
160-
rhs.moved_from_ = true;
161-
162150
*v_ = *rhs.v_;
163-
moved_from_ = false;
164151

165152
return *this;
166153
}
@@ -172,12 +159,10 @@ class LifetimeIterator {
172159
return *v_;
173160
}
174161

175-
Reference& operator=(Value v) {
162+
Reference operator=(Value v) const {
176163
assert(lifetime_cache.contains(this));
177164
assert(!moved_from_);
178-
179165
*v_ = v;
180-
moved_from_ = false;
181166

182167
return *this;
183168
}
@@ -366,40 +351,30 @@ class ConstexprIterator {
366351
constexpr Reference(int& v) : v_(&v) { }
367352

368353
constexpr Reference(const Reference& rhs) = default;
369-
constexpr Reference& operator=(const Reference& rhs) {
354+
constexpr Reference operator=(const Reference& rhs) const {
370355
assert(!rhs.moved_from_);
371-
v_ = rhs.v_;
372-
moved_from_ = false;
356+
assert(!moved_from_);
357+
*v_ = *rhs.v_;
373358

374359
return *this;
375360
}
361+
constexpr Reference operator=(int v) const {
362+
assert(!moved_from_);
363+
*v_ = v;
376364

377-
constexpr Reference(Reference&& rhs) noexcept : v_(rhs.v_) {
378-
assert(!rhs.moved_from_);
379-
rhs.moved_from_ = true;
365+
return *this;
380366
}
381367

382-
constexpr Reference& operator=(Reference&& rhs) noexcept {
368+
constexpr Reference(Reference&& rhs) noexcept : v_(rhs.v_) {
383369
assert(!rhs.moved_from_);
384370
rhs.moved_from_ = true;
385-
moved_from_ = false;
386-
387-
v_ = rhs.v_;
388-
return *this;
389371
}
390372

391373
constexpr operator int() const {
392374
assert(!moved_from_);
393375
return *v_;
394376
}
395377

396-
constexpr Reference& operator=(int v) {
397-
*v_ = v;
398-
moved_from_ = false;
399-
400-
return *this;
401-
}
402-
403378
friend constexpr bool operator<(const Reference& lhs, const Reference& rhs) {
404379
assert(!lhs.moved_from_ && !rhs.moved_from_);
405380
return *lhs.v_ < *rhs.v_;

0 commit comments

Comments
 (0)