• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <tuple>
11 
12 // template <class... Types> class tuple;
13 
14 // constexpr tuple();
15 
16 // UNSUPPORTED: c++98, c++03
17 
18 #include <tuple>
19 #include <string>
20 #include <cassert>
21 #include <type_traits>
22 
23 #include "DefaultOnly.h"
24 
25 struct NoDefault {
26     NoDefault() = delete;
NoDefaultNoDefault27     explicit NoDefault(int) { }
28 };
29 
30 struct NoExceptDefault {
31     NoExceptDefault() noexcept = default;
32 };
33 
34 struct ThrowingDefault {
ThrowingDefaultThrowingDefault35     ThrowingDefault() { }
36 };
37 
38 struct IllFormedDefault {
IllFormedDefaultIllFormedDefault39     IllFormedDefault(int x) : value(x) {}
40     template <bool Pred = false>
IllFormedDefaultIllFormedDefault41     constexpr IllFormedDefault() {
42         static_assert(Pred,
43             "The default constructor should not be instantiated");
44     }
45     int value;
46 };
47 
main()48 int main()
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