Skip to content

Commit

Permalink
<deque>: Use _Next_iter and _Prev_iter (#1161)
Browse files Browse the repository at this point in the history
  • Loading branch information
CaseyCarter authored Aug 12, 2020
1 parent 7f29487 commit ae1068e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
12 changes: 6 additions & 6 deletions stl/inc/deque
Original file line number Diff line number Diff line change
Expand Up @@ -836,10 +836,10 @@ public:

if (_Off <= _Mysize() / 2) { // closer to front, push to front then rotate
emplace_front(_STD forward<_Valty>(_Val)...);
_STD rotate(begin(), begin() + 1, begin() + static_cast<difference_type>(1 + _Off));
_STD rotate(begin(), _Next_iter(begin()), begin() + static_cast<difference_type>(1 + _Off));
} else { // closer to back, push to back then rotate
emplace_back(_STD forward<_Valty>(_Val)...);
_STD rotate(begin() + static_cast<difference_type>(_Off), end() - 1, end());
_STD rotate(begin() + static_cast<difference_type>(_Off), _Prev_iter(end()), end());
}
return begin() + static_cast<difference_type>(_Off);
}
Expand Down Expand Up @@ -1064,15 +1064,15 @@ public:
_STL_VERIFY(!empty(), "back() called on empty deque");
#endif // _CONTAINER_DEBUG_LEVEL > 0

return *(_Unchecked_end() - 1);
return *_Prev_iter(_Unchecked_end());
}

_NODISCARD const_reference back() const noexcept /* strengthened */ {
#if _CONTAINER_DEBUG_LEVEL > 0
_STL_VERIFY(!empty(), "back() called on empty deque");
#endif // _CONTAINER_DEBUG_LEVEL > 0

return *(_Unchecked_end() - 1);
return *_Prev_iter(_Unchecked_end());
}

void push_front(const _Ty& _Val) {
Expand Down Expand Up @@ -1197,10 +1197,10 @@ public:

if (_Off <= _Mysize() / 2) { // closer to front, push to front then copy
push_front(_Val);
_STD rotate(begin(), begin() + 1, begin() + static_cast<difference_type>(1 + _Off));
_STD rotate(begin(), _Next_iter(begin()), begin() + static_cast<difference_type>(1 + _Off));
} else { // closer to back, push to back then copy
push_back(_Val);
_STD rotate(begin() + static_cast<difference_type>(_Off), end() - 1, end());
_STD rotate(begin() + static_cast<difference_type>(_Off), _Prev_iter(end()), end());
}

return begin() + static_cast<difference_type>(_Off);
Expand Down
19 changes: 18 additions & 1 deletion tests/std/tests/Dev10_500860_overloaded_address_of/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ constexpr bool can_addressof = false;
template <typename T>
constexpr bool can_addressof<T, void_t<decltype(addressof(declval<T>()))>> = true;

void test_LWG_2598() {
void test_LWG_2598() { // COMPILE-ONLY
STATIC_ASSERT(can_addressof<int&>);
STATIC_ASSERT(can_addressof<const int&>);
STATIC_ASSERT(can_addressof<volatile int&>);
Expand All @@ -292,3 +292,20 @@ void test_LWG_2598() {
STATIC_ASSERT(!can_addressof<volatile int>);
STATIC_ASSERT(!can_addressof<const volatile int>);
}

// Also test DevCom-1134328, in which `deque<S*>::_Unchecked_iterator{} - 1` finds
// operator-(const S&, int) by argument-dependent lookup causing overload resolution
// to fail due to ambiguity when compiling 64-bit.
struct S {
S() = default;

template <typename T>
S(T&&);
};

S operator-(const S&, int);

void test_DevCom_1134328() { // COMPILE-ONLY
deque<S*> d{nullptr};
(void) d.back();
}

0 comments on commit ae1068e

Please sign in to comment.