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