brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.5 KiB · ea62f76 Raw
67 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 const T& optional<T>::value() const &;14 15#include <optional>16#include <type_traits>17#include <cassert>18 19#include "test_macros.h"20 21using std::optional;22using std::in_place_t;23using std::in_place;24using std::bad_optional_access;25 26struct X27{28    X() = default;29    X(const X&) = delete;30    constexpr int test() const & {return 3;}31    int test() & {return 4;}32    constexpr int test() const && {return 5;}33    int test() && {return 6;}34};35 36int main(int, char**)37{38    {39        const optional<X> opt; ((void)opt);40        ASSERT_NOT_NOEXCEPT(opt.value());41        ASSERT_SAME_TYPE(decltype(opt.value()), X const&);42    }43    {44        constexpr optional<X> opt(in_place);45        static_assert(opt.value().test() == 3, "");46    }47    {48        const optional<X> opt(in_place);49        assert(opt.value().test() == 3);50    }51#ifndef TEST_HAS_NO_EXCEPTIONS52    {53        const optional<X> opt;54        try55        {56            (void)opt.value();57            assert(false);58        }59        catch (const bad_optional_access&)60        {61        }62    }63#endif64 65  return 0;66}67