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 // UNSUPPORTED: c++98, c++03, c++11 11 12 #include <tuple> 13 #include <utility> 14 #include <memory> 15 #include <string> 16 #include <complex> 17 #include <type_traits> 18 19 #include <cassert> 20 21 int main() 22 { 23 typedef std::complex<float> cf; 24 { 25 auto t1 = std::tuple<int, std::string, cf> { 42, "Hi", { 1,2 }}; 26 assert ( std::get<int>(t1) == 42 ); // find at the beginning 27 assert ( std::get<std::string>(t1) == "Hi" ); // find in the middle 28 assert ( std::get<cf>(t1).real() == 1 ); // find at the end 29 assert ( std::get<cf>(t1).imag() == 2 ); 30 } 31 32 { 33 auto t2 = std::tuple<int, std::string, int, cf> { 42, "Hi", 23, { 1,2 }}; 34 // get<int> would fail! 35 assert ( std::get<std::string>(t2) == "Hi" ); 36 assert (( std::get<cf>(t2) == cf{ 1,2 } )); 37 } 38 39 { 40 constexpr std::tuple<int, const int, double, double> p5 { 1, 2, 3.4, 5.6 }; 41 static_assert ( std::get<int>(p5) == 1, "" ); 42 static_assert ( std::get<const int>(p5) == 2, "" ); 43 } 44 45 { 46 const std::tuple<int, const int, double, double> p5 { 1, 2, 3.4, 5.6 }; 47 const int &i1 = std::get<int>(p5); 48 const int &i2 = std::get<const int>(p5); 49 assert ( i1 == 1 ); 50 assert ( i2 == 2 ); 51 } 52 53 { 54 typedef std::unique_ptr<int> upint; 55 std::tuple<upint> t(upint(new int(4))); 56 upint p = std::get<upint>(std::move(t)); // get rvalue 57 assert(*p == 4); 58 assert(std::get<upint>(t) == nullptr); // has been moved from 59 } 60 61 { 62 typedef std::unique_ptr<int> upint; 63 const std::tuple<upint> t(upint(new int(4))); 64 const upint&& p = std::get<upint>(std::move(t)); // get const rvalue 65 assert(*p == 4); 66 assert(std::get<upint>(t) != nullptr); 67 } 68 69 { 70 int x = 42; 71 int y = 43; 72 std::tuple<int&, int const&> const t(x, y); 73 static_assert(std::is_same<int&, decltype(std::get<int&>(std::move(t)))>::value, ""); 74 static_assert(noexcept(std::get<int&>(std::move(t))), ""); 75 static_assert(std::is_same<int const&, decltype(std::get<int const&>(std::move(t)))>::value, ""); 76 static_assert(noexcept(std::get<int const&>(std::move(t))), ""); 77 } 78 79 { 80 int x = 42; 81 int y = 43; 82 std::tuple<int&&, int const&&> const t(std::move(x), std::move(y)); 83 static_assert(std::is_same<int&&, decltype(std::get<int&&>(std::move(t)))>::value, ""); 84 static_assert(noexcept(std::get<int&&>(std::move(t))), ""); 85 static_assert(std::is_same<int const&&, decltype(std::get<int const&&>(std::move(t)))>::value, ""); 86 static_assert(noexcept(std::get<int const&&>(std::move(t))), ""); 87 } 88 89 { 90 constexpr const std::tuple<int, const int, double, double> t { 1, 2, 3.4, 5.6 }; 91 static_assert(std::get<int>(std::move(t)) == 1, ""); 92 static_assert(std::get<const int>(std::move(t)) == 2, ""); 93 } 94 } 95