xref: /llvm-project/libcxx/test/std/experimental/memory/memory.observer.ptr/operator-bool.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 // constexpr explicit operator bool() const noexcept;
18 
19 #include <experimental/memory>
20 #include <type_traits>
21 #include <cassert>
22 
23 template <class T, class Object = T>
test_operator_bool()24 constexpr void test_operator_bool() {
25   using Ptr = std::experimental::observer_ptr<T>;
26   Object obj;
27 
28   {
29     Ptr const ptr(&obj);
30     bool b = static_cast<bool>(ptr);
31     assert(b);
32 
33     static_assert(noexcept(static_cast<bool>(ptr)));
34   }
35 
36   {
37     Ptr const ptr(nullptr);
38     bool b = static_cast<bool>(ptr);
39     assert(!b);
40   }
41 
42   static_assert(!std::is_convertible<Ptr const, bool>::value);
43   static_assert(std::is_constructible<bool, Ptr const>::value);
44 }
45 
46 struct Bar {};
47 
test()48 constexpr bool test() {
49   test_operator_bool<Bar>();
50   test_operator_bool<int>();
51   test_operator_bool<void, int>();
52 
53   return true;
54 }
55 
main(int,char **)56 int main(int, char**) {
57   test();
58   static_assert(test());
59 
60   return 0;
61 }
62