brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.6 KiB · 96d2274 Raw
80 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// constexpr T* optional<T>::operator->();13 14#include <optional>15#include <type_traits>16#include <cassert>17 18#include "test_macros.h"19 20using std::optional;21 22struct X {23  int test() noexcept { return 3; }24  int test() const noexcept { return 3; }25};26 27struct Y28{29    constexpr int test() {return 3;}30};31 32constexpr int33test()34{35    optional<Y> opt{Y{}};36    return opt->test();37}38 39int main(int, char**)40{41    {42        std::optional<X> opt; ((void)opt);43        ASSERT_SAME_TYPE(decltype(opt.operator->()), X*);44        ASSERT_NOEXCEPT(opt.operator->());45    }46    {47        optional<X> opt(X{});48        assert(opt->test() == 3);49    }50#if TEST_STD_VER >= 2651    {52      X x{};53      std::optional<X&> opt(x);54      ASSERT_SAME_TYPE(decltype(opt.operator->()), X*);55      ASSERT_NOEXCEPT(opt.operator->());56    }57    {58      X x{};59      std::optional<const X&> opt(x);60      ASSERT_SAME_TYPE(decltype(opt.operator->()), const X*);61      ASSERT_NOEXCEPT(opt.operator->());62    }63    {64      X x{};65      optional<X&> opt{x};66      assert(opt->test() == 3);67    }68    {69      X x{};70      optional<const X&> opt{x};71      assert(opt->test() == 3);72    }73#endif74    {75        static_assert(test() == 3, "");76    }77 78    return 0;79}80