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