brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.8 KiB · 67d0fcf Raw
115 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 optional(const T& v);14 15#include <cassert>16#include <optional>17#include <type_traits>18 19#include "test_macros.h"20#include "archetypes.h"21 22using std::optional;23 24int main(int, char**) {25  {26    typedef int T;27    constexpr T t(5);28    constexpr optional<T> opt(t);29    static_assert(static_cast<bool>(opt) == true, "");30    static_assert(*opt == 5, "");31 32    struct test_constexpr_ctor : public optional<T> {33      constexpr test_constexpr_ctor(const T&) {}34    };35  }36  {37    typedef double T;38    constexpr T t(3);39    constexpr optional<T> opt(t);40    static_assert(static_cast<bool>(opt) == true, "");41    static_assert(*opt == 3, "");42 43    struct test_constexpr_ctor : public optional<T> {44      constexpr test_constexpr_ctor(const T&) {}45    };46  }47  {48    const int x = 42;49    optional<const int> o(x);50    assert(*o == x);51  }52  {53    typedef TestTypes::TestType T;54    T::reset();55    const T t(3);56    optional<T> opt = t;57    assert(T::alive == 2);58    assert(T::copy_constructed == 1);59    assert(static_cast<bool>(opt) == true);60    assert(opt.value().value == 3);61  }62  {63    typedef ExplicitTestTypes::TestType T;64    static_assert(!std::is_convertible<T const&, optional<T>>::value, "");65    T::reset();66    const T t(3);67    optional<T> opt(t);68    assert(T::alive == 2);69    assert(T::copy_constructed == 1);70    assert(static_cast<bool>(opt) == true);71    assert(opt.value().value == 3);72  }73  {74    typedef ConstexprTestTypes::TestType T;75    constexpr T t(3);76    constexpr optional<T> opt = {t};77    static_assert(static_cast<bool>(opt) == true, "");78    static_assert(opt.value().value == 3, "");79 80    struct test_constexpr_ctor : public optional<T> {81      constexpr test_constexpr_ctor(const T&) {}82    };83  }84  {85    typedef ExplicitConstexprTestTypes::TestType T;86    static_assert(!std::is_convertible<const T&, optional<T>>::value, "");87    constexpr T t(3);88    constexpr optional<T> opt(t);89    static_assert(static_cast<bool>(opt) == true, "");90    static_assert(opt.value().value == 3, "");91 92    struct test_constexpr_ctor : public optional<T> {93      constexpr test_constexpr_ctor(const T&) {}94    };95  }96#ifndef TEST_HAS_NO_EXCEPTIONS97  {98    struct Z {99      Z(int) {}100      Z(const Z&) { throw 6; }101    };102    typedef Z T;103    try {104      const T t(3);105      optional<T> opt(t);106      assert(false);107    } catch (int i) {108      assert(i == 6);109    }110  }111#endif112 113  return 0;114}115