// { dg-do run } // Test the problematic cases identified in PR libstdc++/109150 // where the previous std::fill_n was non-conforming. #include #include const int global = 0; struct X { void operator=(const int& i) { VERIFY(&i == &global); } }; void test_identity_matters() { X x; // Assigning int to X has non-trivial side effects, so we cannot // hoist the load outside the loop, we have to do exactly what the // standard says to do. std::fill_n(&x, 1, global); } struct Y { int i; void operator=(int ii) { i = ii + 1; } }; void test_self_aliasing() { Y y[2] = { }; // Assigning int to X has non-trivial side effects, altering the value // used to fill the later elements. Must not load it outside the loop. std::fill_n(y, 2, y[0].i); VERIFY(y[1].i == 2); } struct Z { Z() { } #if __cplusplus >= 201103L explicit Z(const Z&) = default; #endif }; void test_explicit_copy_ctor() { Z z; // The optimization that copies the fill value must use direct-initialization // otherwise this case would be ill-formed due to the explicit constructor. std::fill_n(&z, 1, z); } int main() { test_identity_matters(); test_self_aliasing(); test_explicit_copy_ctor(); }