xref: /llvm-project/libcxx/test/std/experimental/memory/memory.observer.ptr/ctor.convert.pass.cpp (revision 7a62bee611f1c451fa026c146b03a3a277a5a1dd)
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++03, c++11, c++14
11 // REQUIRES: c++experimental
12 
13 // <experimental/memory>
14 
15 // observer_ptr
16 //
17 // template <class W2>
18 // constexpr observer_ptr(observer_ptr<W2> other) noexcept;
19 
20 #include <experimental/memory>
21 #include <type_traits>
22 #include <cassert>
23 
24 #include "test_macros.h"
25 
26 template <class To, class From>
test_converting_ctor()27 constexpr void test_converting_ctor() {
28   using ToPtr   = std::experimental::observer_ptr<To>;
29   using FromPtr = std::experimental::observer_ptr<From>;
30   From obj;
31   FromPtr from(&obj);
32   ToPtr to = from;
33 
34   assert(from.get() == &obj);
35   assert(to.get() == &obj);
36 #if TEST_STD_VER >= 20
37   static_assert(std::is_nothrow_convertible<FromPtr, ToPtr>::value);
38 #endif
39 }
40 
41 template <class To, class From>
check_non_constructible()42 constexpr void check_non_constructible() {
43   using ToPtr   = std::experimental::observer_ptr<To>;
44   using FromPtr = std::experimental::observer_ptr<From>;
45   static_assert(!std::is_constructible<ToPtr, FromPtr>::value);
46 }
47 
48 struct Bar {};
49 struct Base {};
50 struct Derived : Base {};
51 
test()52 constexpr bool test() {
53   test_converting_ctor<void, Bar>();
54   test_converting_ctor<void, int>();
55   test_converting_ctor<Base, Derived>();
56 
57   check_non_constructible<Derived, Base>();
58   check_non_constructible<int, void>();
59   check_non_constructible<Bar, void>();
60   check_non_constructible<int, long>();
61   check_non_constructible<long, int>();
62 
63   // Check const-ness
64   check_non_constructible<Bar, Bar const>();
65   test_converting_ctor<Bar const, Bar>();
66 
67   return true;
68 }
69 
main(int,char **)70 int main(int, char**) {
71   test();
72   static_assert(test());
73 
74   return 0;
75 }
76