Debian Codegraph @

libstdc++-v3/testsuite/25_algorithms/fill_n/109150.cc

raw
1 // { dg-do run }
2
3 // Test the problematic cases identified in PR libstdc++/109150
4 // where the previous std::fill_n was non-conforming.
5
6 #include <algorithm>
7 #include <testsuite_hooks.h>
8
9 const int global = 0;
10
11 struct X {
12 void operator=(const int& i) { VERIFY(&i == &global); }
13 };
14
15 void
16 test_identity_matters()
17 {
18 X x;
19 // Assigning int to X has non-trivial side effects, so we cannot
20 // hoist the load outside the loop, we have to do exactly what the
21 // standard says to do.
22 std::fill_n(&x, 1, global);
23 }
24
25 struct Y {
26 int i;
27 void operator=(int ii) { i = ii + 1; }
28 };
29
30 void
31 test_self_aliasing()
32 {
33 Y y[2] = { };
34 // Assigning int to X has non-trivial side effects, altering the value
35 // used to fill the later elements. Must not load it outside the loop.
36 std::fill_n(y, 2, y[0].i);
37 VERIFY(y[1].i == 2);
38 }
39
40 struct Z
41 {
42 Z() { }
43 #if __cplusplus >= 201103L
44 explicit Z(const Z&) = default;
45 #endif
46 };
47
48 void
49 test_explicit_copy_ctor()
50 {
51 Z z;
52 // The optimization that copies the fill value must use direct-initialization
53 // otherwise this case would be ill-formed due to the explicit constructor.
54 std::fill_n(&z, 1, z);
55 }
56
57 int main()
58 {
59 test_identity_matters();
60 test_self_aliasing();
61 test_explicit_copy_ctor();
62 }