| 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 | // <optional> |
| 11 | |
| 12 | // template <class T, class... Args> |
| 13 | // constexpr optional<T> make_optional(Args&&... args); |
| 14 | |
| 15 | // GCC crashes on this file, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120577 |
| 16 | // XFAIL: gcc-15 |
| 17 | |
| 18 | #include <optional> |
| 19 | #include <string> |
| 20 | #include <memory> |
| 21 | #include <cassert> |
| 22 | |
| 23 | #include "test_macros.h" |
| 24 | |
| 25 | int main(int, char**) |
| 26 | { |
| 27 | { |
| 28 | constexpr auto opt = std::make_optional<int>('a'); |
| 29 | static_assert(*opt == int('a')); |
| 30 | } |
| 31 | { |
| 32 | std::string s = "123" ; |
| 33 | auto opt = std::make_optional<std::string>(s); |
| 34 | assert(*opt == "123" ); |
| 35 | } |
| 36 | { |
| 37 | std::unique_ptr<int> s = std::make_unique<int>(3); |
| 38 | auto opt = std::make_optional<std::unique_ptr<int>>(std::move(s)); |
| 39 | assert(**opt == 3); |
| 40 | assert(s == nullptr); |
| 41 | } |
| 42 | { |
| 43 | auto opt = std::make_optional<std::string>(4u, 'X'); |
| 44 | assert(*opt == "XXXX" ); |
| 45 | } |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |