131 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(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 24class Z {25public:26 Z(int) {}27 Z(Z&&) { TEST_THROW(6); }28};29 30int main(int, char**) {31 {32 typedef int T;33 constexpr optional<T> opt(T(5));34 static_assert(static_cast<bool>(opt) == true, "");35 static_assert(*opt == 5, "");36 37 struct test_constexpr_ctor : public optional<T> {38 constexpr test_constexpr_ctor(T&&) {}39 };40 }41 {42 typedef double T;43 constexpr optional<T> opt(T(3));44 static_assert(static_cast<bool>(opt) == true, "");45 static_assert(*opt == 3, "");46 47 struct test_constexpr_ctor : public optional<T> {48 constexpr test_constexpr_ctor(T&&) {}49 };50 }51 {52 const int x = 42;53 optional<const int> o(std::move(x));54 assert(*o == 42);55 }56 {57 typedef TestTypes::TestType T;58 T::reset();59 optional<T> opt = T{3};60 assert(T::alive == 1);61 assert(T::move_constructed == 1);62 assert(static_cast<bool>(opt) == true);63 assert(opt.value().value == 3);64 }65 {66 typedef ExplicitTestTypes::TestType T;67 static_assert(!std::is_convertible<T&&, optional<T>>::value, "");68 T::reset();69 optional<T> opt(T{3});70 assert(T::alive == 1);71 assert(T::move_constructed == 1);72 assert(static_cast<bool>(opt) == true);73 assert(opt.value().value == 3);74 }75 {76 typedef TestTypes::TestType T;77 T::reset();78 optional<T> opt = {3};79 assert(T::alive == 1);80 assert(T::value_constructed == 1);81 assert(T::copy_constructed == 0);82 assert(T::move_constructed == 0);83 assert(static_cast<bool>(opt) == true);84 assert(opt.value().value == 3);85 }86 {87 typedef ConstexprTestTypes::TestType T;88 constexpr optional<T> opt = {T(3)};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 {97 typedef ConstexprTestTypes::TestType T;98 constexpr optional<T> opt = {3};99 static_assert(static_cast<bool>(opt) == true, "");100 static_assert(opt.value().value == 3, "");101 102 struct test_constexpr_ctor : public optional<T> {103 constexpr test_constexpr_ctor(const T&) {}104 };105 }106 {107 typedef ExplicitConstexprTestTypes::TestType T;108 static_assert(!std::is_convertible<T&&, optional<T>>::value, "");109 constexpr optional<T> opt(T{3});110 static_assert(static_cast<bool>(opt) == true, "");111 static_assert(opt.value().value == 3, "");112 113 struct test_constexpr_ctor : public optional<T> {114 constexpr test_constexpr_ctor(T&&) {}115 };116 }117#ifndef TEST_HAS_NO_EXCEPTIONS118 {119 try {120 Z z(3);121 optional<Z> opt(std::move(z));122 assert(false);123 } catch (int i) {124 assert(i == 6);125 }126 }127#endif128 129 return 0;130}131