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// #include <memory>
12
13// template<size_t N, class T>
14// [[nodiscard]] constexpr T* assume_aligned(T* ptr);
15
16#include <memory>
17#include <cassert>
18#include <cstddef>
19
20#include "test_macros.h"
21
22template <typename T>
23constexpr void check(T* p) {
24 ASSERT_SAME_TYPE(T*, decltype(std::assume_aligned<1>(p)));
25 constexpr std::size_t alignment = alignof(T);
26
27 if constexpr (alignment >= 1)
28 assert(p == std::assume_aligned<1>(p));
29 if constexpr (alignment >= 2)
30 assert(p == std::assume_aligned<2>(p));
31 if constexpr (alignment >= 4)
32 assert(p == std::assume_aligned<4>(p));
33 if constexpr (alignment >= 8)
34 assert(p == std::assume_aligned<8>(p));
35 if constexpr (alignment >= 16)
36 assert(p == std::assume_aligned<16>(p));
37 if constexpr (alignment >= 32)
38 assert(p == std::assume_aligned<32>(p));
39 if constexpr (alignment >= 64)
40 assert(p == std::assume_aligned<64>(p));
41 if constexpr (alignment >= 128)
42 assert(p == std::assume_aligned<128>(p));
43}
44
45struct S { };
46struct alignas( 4) S4 { };
47struct alignas( 8) S8 { };
48struct alignas( 16) S16 { };
49struct alignas( 32) S32 { };
50struct alignas( 64) S64 { };
51struct alignas(128) S128 { };
52
53constexpr bool tests() {
54 char c;
55 int i;
56 long l;
57 double d;
58 long double ld;
59 check( p: &c);
60 check( p: &i);
61 check( p: &l);
62 check( p: &d);
63 check(p: &ld);
64
65 S s;
66 S4 s4;
67 S8 s8;
68 S16 s16;
69 S32 s32;
70 S64 s64;
71 S128 s128;
72 check(p: &s);
73 check(p: &s4);
74 check(p: &s8);
75 check(p: &s16);
76 check(p: &s32);
77 check(p: &s64);
78 check(p: &s128);
79
80 return true;
81}
82
83int main(int, char**) {
84 tests();
85 static_assert(tests());
86
87 return 0;
88}
89

source code of libcxx/test/std/utilities/memory/ptr.align/assume_aligned.pass.cpp