62 lines · cpp
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-exception7//8//===----------------------------------------------------------------------===//9 10// UNSUPPORTED: c++03, c++11, c++1411// REQUIRES: c++experimental12 13// <experimental/memory>14 15// observer_ptr16//17// constexpr explicit operator bool() const noexcept;18 19#include <experimental/memory>20#include <type_traits>21#include <cassert>22 23template <class T, class Object = T>24constexpr 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 46struct Bar {};47 48constexpr bool test() {49 test_operator_bool<Bar>();50 test_operator_bool<int>();51 test_operator_bool<void, int>();52 53 return true;54}55 56int main(int, char**) {57 test();58 static_assert(test());59 60 return 0;61}62