| 1 |
// { dg-do run { target c++11 } } |
| 2 |
// { dg-skip-if "requires hosted libstdc++ for iostream" { ! hostedlib } } |
| 3 |
// PR rtl-optimization/95493 comment 8 |
| 4 |
|
| 5 |
#include <array> |
| 6 |
#include <iostream> |
| 7 |
|
| 8 |
struct Point |
| 9 |
{ |
| 10 |
std::array<int, 3> array; |
| 11 |
|
| 12 |
Point(int x, int y, int z) : array{x, y, z} {} |
| 13 |
|
| 14 |
Point(const Point & other) : array{other.array} {} // OK if commented |
| 15 |
//Point(const Point &) = default; // OK |
| 16 |
|
| 17 |
//Point(Point && other) = default; // OK |
| 18 |
|
| 19 |
int operator[] (std::size_t i) const { return array[i]; } |
| 20 |
int& operator[] (std::size_t i) { return array[i]; } |
| 21 |
}; |
| 22 |
|
| 23 |
//using Point = std::array<int, 3>; // OK |
| 24 |
|
| 25 |
struct Cell |
| 26 |
{ |
| 27 |
Point point; |
| 28 |
Cell(Point const& pt) : point(pt) {} |
| 29 |
int operator[] (std::size_t i) const { return point[i]; } |
| 30 |
int& operator[] (std::size_t i) { return point[i]; } |
| 31 |
}; |
| 32 |
|
| 33 |
//using Cell = Point; // OK |
| 34 |
|
| 35 |
std::ostream & operator<< (std::ostream & out, Cell const& object) |
| 36 |
//std::ostream & operator<< (std::ostream & out, Cell object) // Fails with f2 too |
| 37 |
{ |
| 38 |
for ( std::size_t i = 0; i < 3; ++i ) |
| 39 |
out << object[ i ] << " "; |
| 40 |
return out; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
struct DirIterator |
| 45 |
{ |
| 46 |
std::size_t dir; |
| 47 |
Cell cell; |
| 48 |
|
| 49 |
DirIterator(Cell c) |
| 50 |
: dir(0), cell(c) |
| 51 |
{ |
| 52 |
find(); // OK if commented |
| 53 |
} |
| 54 |
|
| 55 |
void find() |
| 56 |
{ |
| 57 |
//while (dir < 3) // Fails with f2 too |
| 58 |
while (dir < 3 && (cell[dir] % 2) == 0) |
| 59 |
++dir; |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
Cell uIncident(Cell c, std::size_t k) |
| 64 |
//Cell uIncident(Cell& c, std::size_t k) // OK |
| 65 |
{ |
| 66 |
--c[k]; |
| 67 |
return c; |
| 68 |
} |
| 69 |
|
| 70 |
Cell uSpel(Point p) |
| 71 |
{ |
| 72 |
for (std::size_t i = 0; i < 3; ++i) |
| 73 |
p[i] += p[i] + 1; |
| 74 |
return Cell(p); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
int main () |
| 79 |
{ |
| 80 |
Cell c = uSpel(Point{0, 0, 0}); // Fails |
| 81 |
//Cell c( Point(1, 1, 1) ); // OK |
| 82 |
|
| 83 |
auto q = DirIterator( c ); |
| 84 |
|
| 85 |
Cell f1 = uIncident( c, q.dir ); // Fails |
| 86 |
//Cell f1 = uIncident( c, 0 ); // OK |
| 87 |
|
| 88 |
Cell f2 = f1; // f2 is the right cell that f1 should be |
| 89 |
|
| 90 |
std::cout << "q.dir = " << q.dir << " ; f1 = " << f1 << " ; f2 = " << f2 << std::endl; |
| 91 |
//std::cout << "q.dir = " << q.dir << " ; f1 = " << f1[0] << " " << f1[1] << " " << f1[2] << " ; f2 = " << f2[0] << " " << f2[1] << " " << f2[2] << std::endl; // OK |
| 92 |
|
| 93 |
for (int i = 0; i < 3; ++i) |
| 94 |
if (f1[i] != f2[i]) |
| 95 |
__builtin_abort(); |
| 96 |
} |