xref: /llvm-project/libcxx/test/std/utilities/memory/pointer.traits/ptr.pointer_to.pass.cpp (revision 029c9c31769c26c53108aba7436d46f9ab63ed8d)
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 // <memory>
10 
11 // template <class T>
12 // struct pointer_traits<T*>
13 // {
14 //     static pointer pointer_to(<details>) noexcept; // constexpr in C++20
15 //     ...
16 // };
17 
18 #include <memory>
19 #include <cassert>
20 #include <type_traits>
21 
22 #include "test_macros.h"
23 
test()24 TEST_CONSTEXPR_CXX20 bool test()
25 {
26     // pointer_traits<T*>
27     {
28         int i = 0;
29         static_assert(std::is_same<decltype(std::pointer_traits<int*>::pointer_to(i)), int*>::value, "");
30         ASSERT_NOEXCEPT(std::pointer_traits<int*>::pointer_to(i));
31         assert(std::pointer_traits<int*>::pointer_to(i) == &i);
32     }
33     // pointer_traits<const T*>
34     {
35         int const i = 0;
36         static_assert(std::is_same<decltype(std::pointer_traits<const int*>::pointer_to(i)), const int*>::value, "");
37         ASSERT_NOEXCEPT(std::pointer_traits<const int*>::pointer_to(i));
38         assert(std::pointer_traits<const int*>::pointer_to(i) == &i);
39     }
40     // pointer_traits<const T*> with a non-const argument
41     {
42         int i = 0;
43         static_assert(std::is_same<decltype(std::pointer_traits<const int*>::pointer_to(i)), const int*>::value, "");
44         ASSERT_NOEXCEPT(std::pointer_traits<const int*>::pointer_to(i));
45         assert(std::pointer_traits<const int*>::pointer_to(i) == &i);
46     }
47     return true;
48 }
49 
main(int,char **)50 int main(int, char**)
51 {
52     test();
53 #if TEST_STD_VER > 17
54     static_assert(test());
55 #endif
56 
57     {
58         // Check that pointer_traits<void*> is still well-formed, even though it has no pointer_to.
59         static_assert(std::is_same<std::pointer_traits<void*>::element_type, void>::value, "");
60         static_assert(std::is_same<std::pointer_traits<const void*>::element_type, const void>::value, "");
61         static_assert(std::is_same<std::pointer_traits<volatile void*>::element_type, volatile void>::value, "");
62     }
63 
64     return 0;
65 }
66