xref: /llvm-project/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_non_const.pass.cpp (revision 31cbe0f240f660f15602c96b787c58a26f17e179)
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 // template <size_t I, class... Types>
14 //   typename tuple_element<I, tuple<Types...> >::type&
15 //   get(tuple<Types...>& t);
16 
17 // UNSUPPORTED: c++03
18 
19 #include <tuple>
20 #include <string>
21 #include <cassert>
22 
23 #include "test_macros.h"
24 
25 #if TEST_STD_VER > 11
26 
27 struct Empty {};
28 
29 struct S {
30    std::tuple<int, Empty> a;
31    int k;
32    Empty e;
SS33    constexpr S() : a{1,Empty{}}, k(std::get<0>(a)), e(std::get<1>(a)) {}
34    };
35 
getP()36 constexpr std::tuple<int, int> getP () { return { 3, 4 }; }
37 #endif
38 
main(int,char **)39 int main(int, char**)
40 {
41     {
42         typedef std::tuple<int> T;
43         T t(3);
44         assert(std::get<0>(t) == 3);
45         std::get<0>(t) = 2;
46         assert(std::get<0>(t) == 2);
47     }
48     {
49         typedef std::tuple<std::string, int> T;
50         T t("high", 5);
51         assert(std::get<0>(t) == "high");
52         assert(std::get<1>(t) == 5);
53         std::get<0>(t) = "four";
54         std::get<1>(t) = 4;
55         assert(std::get<0>(t) == "four");
56         assert(std::get<1>(t) == 4);
57     }
58     {
59         typedef std::tuple<double&, std::string, int> T;
60         double d = 1.5;
61         T t(d, "high", 5);
62         assert(std::get<0>(t) == 1.5);
63         assert(std::get<1>(t) == "high");
64         assert(std::get<2>(t) == 5);
65         std::get<0>(t) = 2.5;
66         std::get<1>(t) = "four";
67         std::get<2>(t) = 4;
68         assert(std::get<0>(t) == 2.5);
69         assert(std::get<1>(t) == "four");
70         assert(std::get<2>(t) == 4);
71         assert(d == 2.5);
72     }
73 #if TEST_STD_VER > 11
74     { // get on an rvalue tuple
75         static_assert ( std::get<0> ( std::make_tuple ( 0.0f, 1, 2.0, 3L )) == 0, "" );
76         static_assert ( std::get<1> ( std::make_tuple ( 0.0f, 1, 2.0, 3L )) == 1, "" );
77         static_assert ( std::get<2> ( std::make_tuple ( 0.0f, 1, 2.0, 3L )) == 2, "" );
78         static_assert ( std::get<3> ( std::make_tuple ( 0.0f, 1, 2.0, 3L )) == 3, "" );
79         static_assert(S().k == 1, "");
80         static_assert(std::get<1>(getP()) == 4, "");
81     }
82 #endif
83 
84 
85   return 0;
86 }
87