xref: /llvm-project/libcxx/test/std/utilities/optional/optional.specalg/make_optional.pass.cpp (revision ec350ad418a24f70c88758259c774a1e11c06d74)
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 // <optional>
12 //
13 // template <class T>
14 //   constexpr optional<decay_t<T>> make_optional(T&& v);
15 
16 #include <optional>
17 #include <string>
18 #include <memory>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 
main(int,char **)23 int main(int, char**)
24 {
25     {
26         int arr[10];
27         auto opt = std::make_optional(arr);
28         ASSERT_SAME_TYPE(decltype(opt), std::optional<int*>);
29         assert(*opt == arr);
30     }
31     {
32         constexpr auto opt = std::make_optional(2);
33         ASSERT_SAME_TYPE(decltype(opt), const std::optional<int>);
34         static_assert(opt.value() == 2);
35     }
36     {
37         auto opt = std::make_optional(2);
38         ASSERT_SAME_TYPE(decltype(opt), std::optional<int>);
39         assert(*opt == 2);
40     }
41     {
42         const std::string s = "123";
43         auto opt = std::make_optional(s);
44         ASSERT_SAME_TYPE(decltype(opt), std::optional<std::string>);
45         assert(*opt == "123");
46     }
47     {
48         std::unique_ptr<int> s = std::make_unique<int>(3);
49         auto opt = std::make_optional(std::move(s));
50         ASSERT_SAME_TYPE(decltype(opt), std::optional<std::unique_ptr<int>>);
51         assert(**opt == 3);
52         assert(s == nullptr);
53     }
54 
55   return 0;
56 }
57