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 // UNSUPPORTED: c++03, c++11, c++14 10 11 // Throwing bad_optional_access is supported starting in macosx10.13 12 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12}} && !no-exceptions 13 14 // <optional> 15 // 16 // template <class T> 17 // constexpr optional<decay_t<T>> make_optional(T&& v); 18 19 #include <optional> 20 #include <string> 21 #include <memory> 22 #include <cassert> 23 24 #include "test_macros.h" 25 26 int main(int, char**) 27 { 28 using std::optional; 29 using std::make_optional; 30 { 31 int arr[10]; ((void)arr); 32 ASSERT_SAME_TYPE(decltype(make_optional(arr)), optional<int*>); 33 } 34 { 35 constexpr auto opt = make_optional(2); 36 ASSERT_SAME_TYPE(decltype(opt), const optional<int>); 37 static_assert(opt.value() == 2); 38 } 39 { 40 optional<int> opt = make_optional(2); 41 assert(*opt == 2); 42 } 43 { 44 std::string s("123"); 45 optional<std::string> opt = make_optional(s); 46 assert(*opt == s); 47 } 48 { 49 std::unique_ptr<int> s(new int(3)); 50 optional<std::unique_ptr<int>> opt = make_optional(std::move(s)); 51 assert(**opt == 3); 52 assert(s == nullptr); 53 } 54 55 return 0; 56 } 57