72 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// UNSUPPORTED: c++03, c++11, c++1410// <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=12057716// XFAIL: gcc-1517 18#include <cassert>19#include <memory>20#include <optional>21#include <string>22#include <string_view>23 24#include "test_macros.h"25 26template <typename T>27constexpr bool test_ref() {28 T i{0};29 auto opt = std::make_optional<T&>(i);30 31#if TEST_STD_VER < 2632 assert((std::is_same_v<decltype(opt), std::optional<T>>));33#else34 assert((std::is_same_v<decltype(opt), std::optional<T&>>));35#endif36 37 assert(*opt == 0);38 39 return true;40}41 42int main(int, char**)43{44 {45 constexpr auto opt = std::make_optional<int>('a');46 static_assert(*opt == int('a'));47 }48 {49 std::string s = "123";50 auto opt = std::make_optional<std::string>(s);51 assert(*opt == "123");52 }53 {54 std::unique_ptr<int> s = std::make_unique<int>(3);55 auto opt = std::make_optional<std::unique_ptr<int>>(std::move(s));56 assert(**opt == 3);57 assert(s == nullptr);58 }59 {60 auto opt = std::make_optional<std::string>(4u, 'X');61 assert(*opt == "XXXX");62 }63 using namespace std::string_view_literals;64 65 static_assert(test_ref<int>());66 assert((test_ref<int>()));67 static_assert(test_ref<double>());68 assert((test_ref<double>()));69 70 return 0;71}72