brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.6 KiB · 22b74f5 Raw
84 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 11// <optional>12 13// constexpr T& optional<T>::value() &;14 15#include <optional>16#include <type_traits>17#include <cassert>18 19#include "test_macros.h"20 21using std::optional;22using std::bad_optional_access;23 24struct X25{26    X() = default;27    X(const X&) = delete;28    constexpr int test() const & {return 3;}29    int test() & {return 4;}30    constexpr int test() const && {return 5;}31    int test() && {return 6;}32};33 34struct Y35{36    constexpr int test() & {return 7;}37};38 39constexpr int40test()41{42    optional<Y> opt{Y{}};43    return opt.value().test();44}45 46 47int main(int, char**)48{49    {50        optional<X> opt; ((void)opt);51        ASSERT_NOT_NOEXCEPT(opt.value());52        ASSERT_SAME_TYPE(decltype(opt.value()), X&);53    }54    {55        optional<X> opt;56        opt.emplace();57        assert(opt.value().test() == 4);58    }59#if TEST_STD_VER >= 2660    {61      X x;62      optional<X&> opt{x};63      ASSERT_NOT_NOEXCEPT(opt.value());64      ASSERT_SAME_TYPE(decltype(opt.value()), X&);65    }66#endif67#ifndef TEST_HAS_NO_EXCEPTIONS68    {69        optional<X> opt;70        try71        {72            (void)opt.value();73            assert(false);74        }75        catch (const bad_optional_access&)76        {77        }78    }79#endif80    static_assert(test() == 7, "");81 82  return 0;83}84