| 1 | // { dg-do run { target c++11 } } |
| 2 | |
| 3 | #include <queue> |
| 4 | #include <vector> |
| 5 | #include <testsuite_hooks.h> |
| 6 | |
| 7 | template<typename T, typename Seq> |
| 8 | bool |
| 9 | check(std::priority_queue<T, Seq>& p) |
| 10 | { |
| 11 | if (!p.empty()) |
| 12 | { |
| 13 | T prev = p.top(); |
| 14 | p.pop(); |
| 15 | while (!p.empty()) |
| 16 | { |
| 17 | if ( prev < p.top() ) |
| 18 | return false; |
| 19 | prev = p.top(); |
| 20 | p.pop(); |
| 21 | } |
| 22 | } |
| 23 | return true; |
| 24 | } |
| 25 | |
| 26 | // A vector-like type that has a non-empty moved-from state. |
| 27 | struct Vector : std::vector<int> |
| 28 | { |
| 29 | using Base = std::vector<int>; |
| 30 | |
| 31 | using Base::Base; |
| 32 | |
| 33 | Vector(const Vector&) = default; |
| 34 | Vector& operator=(const Vector&) = default; |
| 35 | |
| 36 | Vector(Vector&& v) : Base(static_cast<const Base&>(v)) |
| 37 | { |
| 38 | invalidate_heap(v); |
| 39 | } |
| 40 | |
| 41 | Vector(Vector&& v, const std::allocator<int>&) |
| 42 | : Base(static_cast<const Base&>(v)) |
| 43 | { |
| 44 | invalidate_heap(v); |
| 45 | } |
| 46 | |
| 47 | Vector& |
| 48 | operator=(Vector&& v) |
| 49 | { |
| 50 | static_cast<Base&>(*this) = static_cast<const Base&>(v); |
| 51 | invalidate_heap(v); |
| 52 | return *this; |
| 53 | } |
| 54 | |
| 55 | void invalidate_heap(Base& v) { v = {1,2,3}; } |
| 56 | }; |
| 57 | |
| 58 | void |
| 59 | test_moves() |
| 60 | { |
| 61 | std::priority_queue<int, Vector> p; |
| 62 | p.push(1); |
| 63 | p.push(3); |
| 64 | p.push(5); |
| 65 | p.push(2); |
| 66 | p.push(2); |
| 67 | p.push(2); |
| 68 | p.push(2); |
| 69 | std::priority_queue<int, Vector> p2 = std::move(p); |
| 70 | VERIFY( check(p) ); |
| 71 | |
| 72 | // Allocator-extended move constructor: |
| 73 | std::priority_queue<int, Vector> p3(std::move(p2), std::allocator<int>()); |
| 74 | VERIFY( check(p2) ); |
| 75 | |
| 76 | p2 = std::move(p3); |
| 77 | VERIFY( check(p3) ); |
| 78 | } |
| 79 | |
| 80 | int main() |
| 81 | { |
| 82 | test_moves(); |
| 83 | } |