Debian Codegraph @

libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_default_construct/constexpr.cc

raw
1 // { dg-do compile { target c++26 } }
2
3 #include <algorithm>
4 #include <memory>
5 #include <span>
6 #include <string>
7 #include <vector>
8
9 template<typename T>
10 constexpr
11 bool
12 test01_impl()
13 {
14 static_assert(std::default_initializable<T>);
15 static_assert(std::equality_comparable<T>);
16
17 constexpr std::size_t size = 42;
18 std::allocator<T> alloc;
19 T* ptr = alloc.allocate(size);
20
21 auto check = [&]() -> bool
22 {
23 if constexpr (!std::is_trivially_default_constructible_v<T>)
24 return std::all_of(ptr, ptr + size, [](auto &&x) { return x == T(); });
25 else
26 return true;
27 };
28
29 std::uninitialized_default_construct(ptr, ptr + size);
30 if (!check())
31 return false;
32 std::destroy(ptr, ptr + size);
33
34 std::uninitialized_default_construct_n(ptr, size);
35 if (!check())
36 return false;
37 std::destroy_n(ptr, size);
38
39 std::span<T> storage(ptr, ptr + size);
40 std::ranges::uninitialized_default_construct(storage);
41 if (!check())
42 return false;
43 std::ranges::destroy(storage);
44
45 std::ranges::uninitialized_default_construct_n(ptr, size);
46 if (!check())
47 return false;
48 std::ranges::destroy_n(ptr, size);
49
50 alloc.deallocate(ptr, size);
51 return true;
52 }
53
54 constexpr
55 bool
56 test01()
57 {
58 return
59 test01_impl<char>() &&
60 test01_impl<int>() &&
61 test01_impl<double>() &&
62 #if _GLIBCXX_USE_CXX11_ABI
63 test01_impl<std::string>() &&
64 #endif
65 test01_impl<std::vector<int>>() &&
66 test01_impl<std::unique_ptr<int>>();
67 }
68
69 static_assert(test01());