| 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-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // <tuple> |
| 10 | |
| 11 | // template <class... Types> class tuple; |
| 12 | |
| 13 | // explicit(see-below) constexpr tuple(); |
| 14 | |
| 15 | // UNSUPPORTED: c++03 |
| 16 | |
| 17 | #include <tuple> |
| 18 | #include <string> |
| 19 | #include <cassert> |
| 20 | #include <type_traits> |
| 21 | |
| 22 | #include "test_macros.h" |
| 23 | #include "DefaultOnly.h" |
| 24 | |
| 25 | struct NoDefault { |
| 26 | NoDefault() = delete; |
| 27 | explicit NoDefault(int) { } |
| 28 | }; |
| 29 | |
| 30 | struct NoExceptDefault { |
| 31 | NoExceptDefault() noexcept = default; |
| 32 | }; |
| 33 | |
| 34 | struct ThrowingDefault { |
| 35 | ThrowingDefault() { } |
| 36 | }; |
| 37 | |
| 38 | struct IllFormedDefault { |
| 39 | IllFormedDefault(int x) : value(x) {} |
| 40 | template <bool Pred = false> |
| 41 | constexpr IllFormedDefault() { |
| 42 | static_assert(Pred, |
| 43 | "The default constructor should not be instantiated" ); |
| 44 | } |
| 45 | int value; |
| 46 | }; |
| 47 | |
| 48 | int main(int, char**) |
| 49 | { |
| 50 | { |
| 51 | std::tuple<> t; |
| 52 | (void)t; |
| 53 | } |
| 54 | { |
| 55 | std::tuple<int> t; |
| 56 | assert(std::get<0>(t) == 0); |
| 57 | } |
| 58 | { |
| 59 | std::tuple<int, char*> t; |
| 60 | assert(std::get<0>(t) == 0); |
| 61 | assert(std::get<1>(t) == nullptr); |
| 62 | } |
| 63 | { |
| 64 | std::tuple<int, char*, std::string> t; |
| 65 | assert(std::get<0>(t) == 0); |
| 66 | assert(std::get<1>(t) == nullptr); |
| 67 | assert(std::get<2>(t) == "" ); |
| 68 | } |
| 69 | { |
| 70 | std::tuple<int, char*, std::string, DefaultOnly> t; |
| 71 | assert(std::get<0>(t) == 0); |
| 72 | assert(std::get<1>(t) == nullptr); |
| 73 | assert(std::get<2>(t) == "" ); |
| 74 | assert(std::get<3>(t) == DefaultOnly()); |
| 75 | } |
| 76 | { |
| 77 | // See bug #21157. |
| 78 | static_assert(!std::is_default_constructible<std::tuple<NoDefault>>(), "" ); |
| 79 | static_assert(!std::is_default_constructible<std::tuple<DefaultOnly, NoDefault>>(), "" ); |
| 80 | static_assert(!std::is_default_constructible<std::tuple<NoDefault, DefaultOnly, NoDefault>>(), "" ); |
| 81 | } |
| 82 | { |
| 83 | static_assert(noexcept(std::tuple<NoExceptDefault>()), "" ); |
| 84 | static_assert(noexcept(std::tuple<NoExceptDefault, NoExceptDefault>()), "" ); |
| 85 | |
| 86 | static_assert(!noexcept(std::tuple<ThrowingDefault, NoExceptDefault>()), "" ); |
| 87 | static_assert(!noexcept(std::tuple<NoExceptDefault, ThrowingDefault>()), "" ); |
| 88 | static_assert(!noexcept(std::tuple<ThrowingDefault, ThrowingDefault>()), "" ); |
| 89 | } |
| 90 | { |
| 91 | constexpr std::tuple<> t; |
| 92 | (void)t; |
| 93 | } |
| 94 | { |
| 95 | constexpr std::tuple<int> t; |
| 96 | assert(std::get<0>(t) == 0); |
| 97 | } |
| 98 | { |
| 99 | constexpr std::tuple<int, char*> t; |
| 100 | assert(std::get<0>(t) == 0); |
| 101 | assert(std::get<1>(t) == nullptr); |
| 102 | } |
| 103 | { |
| 104 | // Check that the SFINAE on the default constructor is not evaluated when |
| 105 | // it isn't needed. If the default constructor is evaluated then this test |
| 106 | // should fail to compile. |
| 107 | IllFormedDefault v(0); |
| 108 | std::tuple<IllFormedDefault> t(v); |
| 109 | } |
| 110 | { |
| 111 | struct Base { }; |
| 112 | struct Derived : Base { protected: Derived() = default; }; |
| 113 | static_assert(!std::is_default_constructible<std::tuple<Derived, int> >::value, "" ); |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |