xref: /llvm-project/libcxx/test/std/ranges/range.adaptors/range.drop/dangling.cache.pass.cpp (revision f5832bab6f5024cabe32a9f668b7f44e6b7cfef5)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 
11 // If we have a copy-propagating cache, when we copy ZeroOnDestroy, we will get a
12 // dangling reference to the copied-from object. This test ensures that we do not
13 // propagate the cache on copy.
14 
15 #include <ranges>
16 
17 #include <cstddef>
18 #include <cstring>
19 
20 #include "test_macros.h"
21 #include "types.h"
22 
23 struct ZeroOnDestroy : std::ranges::view_base {
24   unsigned count = 0;
25   int buff[8] = {1, 2, 3, 4, 5, 6, 7, 8};
26 
beginZeroOnDestroy27   constexpr ForwardIter begin() { return ForwardIter(buff); }
beginZeroOnDestroy28   constexpr ForwardIter begin() const { return ForwardIter(); }
endZeroOnDestroy29   constexpr ForwardIter end() { return ForwardIter(buff + 8); }
endZeroOnDestroy30   constexpr ForwardIter end() const { return ForwardIter(); }
31 
32   ZeroOnDestroy() = default;
33   ZeroOnDestroy(const ZeroOnDestroy&) = default;
34   ZeroOnDestroy& operator=(const ZeroOnDestroy&) = default;
~ZeroOnDestroyZeroOnDestroy35   ~ZeroOnDestroy() {
36     std::memset(buff, 0, sizeof(buff));
37   }
38 
dropFirstFourZeroOnDestroy39   static auto dropFirstFour() {
40     ZeroOnDestroy zod;
41     std::ranges::drop_view dv(zod, 4);
42     // Make sure we call begin here so the next call to begin will
43     // use the cached iterator.
44     assert(*dv.begin() == 5);
45     // Intentionally invoke the copy ctor here.
46     return std::ranges::drop_view(dv);
47   }
48 };
49 
main(int,char **)50 int main(int, char**) {
51   auto noDanglingCache = ZeroOnDestroy::dropFirstFour();
52   // If we use the cached version, it will reference the copied-from view.
53   // Worst case this is a segfault, best case it's an assertion fired.
54   assert(*noDanglingCache.begin() == 5);
55 
56   return 0;
57 }
58