94 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <memory>10 11// unique_ptr12 13// Make sure that we can form unique_ptrs to incomplete types and perform restricted14// operations on them. This requires setting up a TU where the type is complete and15// the unique_ptr is created and destroyed, and a TU where the type is incomplete and16// we check that a restricted set of operations can be performed on the unique_ptr.17 18// RUN: %{cxx} %s %{flags} %{compile_flags} -c -o %t.tu1.o -DCOMPLETE19// RUN: %{cxx} %s %{flags} %{compile_flags} -c -o %t.tu2.o -DINCOMPLETE20// RUN: %{cxx} %t.tu1.o %t.tu2.o %{flags} %{link_flags} -o %t.exe21// RUN: %{exec} %t.exe22 23#include <memory>24#include <cassert>25 26struct Foo;27extern void use(std::unique_ptr<Foo>& ptr);28extern void use(std::unique_ptr<Foo[]>& ptr);29 30#ifdef INCOMPLETE31 32void use(std::unique_ptr<Foo>& ptr) {33 {34 Foo* x = ptr.get();35 assert(x != nullptr);36 }37 {38 Foo& ref = *ptr;39 assert(&ref == ptr.get());40 }41 {42 bool engaged = static_cast<bool>(ptr);43 assert(engaged);44 }45 {46 assert(ptr == ptr);47 assert(!(ptr != ptr));48 assert(!(ptr < ptr));49 assert(!(ptr > ptr));50 assert(ptr <= ptr);51 assert(ptr >= ptr);52 }53}54 55void use(std::unique_ptr<Foo[]>& ptr) {56 {57 Foo* x = ptr.get();58 assert(x != nullptr);59 }60 {61 bool engaged = static_cast<bool>(ptr);62 assert(engaged);63 }64 {65 assert(ptr == ptr);66 assert(!(ptr != ptr));67 assert(!(ptr < ptr));68 assert(!(ptr > ptr));69 assert(ptr <= ptr);70 assert(ptr >= ptr);71 }72}73 74#endif // INCOMPLETE75 76#ifdef COMPLETE77 78struct Foo {}; // complete the type79 80int main(int, char**) {81 {82 std::unique_ptr<Foo> ptr(new Foo());83 use(ptr);84 }85 86 {87 std::unique_ptr<Foo[]> ptr(new Foo[3]());88 use(ptr);89 }90 return 0;91}92 93#endif // COMPLETE94