| 1 |
// Copyright (C) 2017-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// { dg-do run { target c++11 } } |
| 19 |
|
| 20 |
#include <algorithm> |
| 21 |
#include <testsuite_hooks.h> |
| 22 |
|
| 23 |
bool values[100]; |
| 24 |
|
| 25 |
unsigned next_id() |
| 26 |
{ |
| 27 |
static unsigned counter = 0; |
| 28 |
VERIFY(counter < 100); |
| 29 |
return counter++; |
| 30 |
} |
| 31 |
|
| 32 |
struct value |
| 33 |
{ |
| 34 |
int val; |
| 35 |
const unsigned id; |
| 36 |
|
| 37 |
value(int i = 0) : val(i), id(next_id()) { values[id] = true; } |
| 38 |
value(const value& v) : val(v.val), id(next_id()) { values[id] = true; } |
| 39 |
value& operator=(const value& v) { val = v.val; return *this; } |
| 40 |
~value() { values[id] = false; } |
| 41 |
}; |
| 42 |
|
| 43 |
bool operator<(const value& lhs, const value& rhs) |
| 44 |
{ |
| 45 |
if (!values[lhs.id]) |
| 46 |
throw lhs.id; |
| 47 |
if (!values[rhs.id]) |
| 48 |
throw rhs.id; |
| 49 |
return lhs.val < rhs.val; |
| 50 |
} |
| 51 |
|
| 52 |
bool operator==(const value& lhs, const value& rhs) |
| 53 |
{ |
| 54 |
if (!values[lhs.id]) |
| 55 |
throw lhs.id; |
| 56 |
if (!values[rhs.id]) |
| 57 |
throw rhs.id; |
| 58 |
return lhs.val == rhs.val; |
| 59 |
} |
| 60 |
|
| 61 |
// A forward iterator that fails to meet the requirement that for any |
| 62 |
// two dereferenceable forward iterators, a == b implies &*a == &*b |
| 63 |
struct stashing_iterator |
| 64 |
{ |
| 65 |
typedef std::forward_iterator_tag iterator_category; |
| 66 |
typedef value value_type; |
| 67 |
typedef value_type const* pointer; |
| 68 |
typedef value_type const& reference; |
| 69 |
typedef std::ptrdiff_t difference_type; |
| 70 |
|
| 71 |
stashing_iterator() : ptr(), stashed() { } |
| 72 |
stashing_iterator(pointer p) : ptr(p), stashed() { stash(); } |
| 73 |
stashing_iterator(const stashing_iterator&) = default; |
| 74 |
stashing_iterator& operator=(const stashing_iterator&) = default; |
| 75 |
|
| 76 |
stashing_iterator& operator++() |
| 77 |
{ |
| 78 |
++ptr; |
| 79 |
stash(); |
| 80 |
return *this; |
| 81 |
} |
| 82 |
|
| 83 |
stashing_iterator operator++(int) |
| 84 |
{ |
| 85 |
stashing_iterator i = *this; |
| 86 |
++*this; |
| 87 |
return i; |
| 88 |
} |
| 89 |
|
| 90 |
reference operator*() const { return stashed; } |
| 91 |
pointer operator->() const { return &**this; } |
| 92 |
|
| 93 |
bool operator==(const stashing_iterator& i) const { return ptr == i.ptr; } |
| 94 |
bool operator!=(const stashing_iterator& i) const { return !(*this == i); } |
| 95 |
|
| 96 |
private: |
| 97 |
void stash() |
| 98 |
{ |
| 99 |
if (ptr) |
| 100 |
stashed = *ptr; |
| 101 |
} |
| 102 |
|
| 103 |
pointer ptr; |
| 104 |
value_type stashed; |
| 105 |
}; |
| 106 |
|
| 107 |
void |
| 108 |
test01() |
| 109 |
{ |
| 110 |
value s[] = { 0, 1, 2, 3, 4, 5 }; |
| 111 |
std::search(s, s+6, stashing_iterator(s), stashing_iterator(s+4)); |
| 112 |
// { dg-warning "ignoring return value" "" { target c++11 } 111 } |
| 113 |
} |
| 114 |
|
| 115 |
int |
| 116 |
main() |
| 117 |
{ |
| 118 |
test01(); |
| 119 |
} |