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 { 29 int arr[10]; 30 auto opt = std::make_optional(arr); 31 ASSERT_SAME_TYPE(decltype(opt), std::optional<int*>); 32 assert(*opt == arr); 33 } 34 { 35 constexpr auto opt = std::make_optional(2); 36 ASSERT_SAME_TYPE(decltype(opt), const std::optional<int>); 37 static_assert(opt.value() == 2); 38 } 39 { 40 auto opt = std::make_optional(2); 41 ASSERT_SAME_TYPE(decltype(opt), std::optional<int>); 42 assert(*opt == 2); 43 } 44 { 45 const std::string s = "123"; 46 auto opt = std::make_optional(s); 47 ASSERT_SAME_TYPE(decltype(opt), std::optional<std::string>); 48 assert(*opt == "123"); 49 } 50 { 51 std::unique_ptr<int> s = std::make_unique<int>(3); 52 auto opt = std::make_optional(std::move(s)); 53 ASSERT_SAME_TYPE(decltype(opt), std::optional<std::unique_ptr<int>>); 54 assert(**opt == 3); 55 assert(s == nullptr); 56 } 57 58 return 0; 59 } 60