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 // <utility>
11
12 // class make_integer_sequence
13
14 #include <tuple>
15 #include <utility>
16 #include <type_traits>
17 #include <cassert>
18
19 #if _LIBCPP_STD_VER > 11
20
21 template <typename AtContainer, typename T, T... I>
extract(const AtContainer & t,const std::integer_sequence<T,I...> idx)22 auto extract ( const AtContainer &t, const std::integer_sequence<T, I...> idx )
23 -> decltype ( std::make_tuple ( std::get<I>(t)... ))
24 { return std::make_tuple ( std::get<I>(t)... ); }
25
26 #endif // _LIBCPP_STD_VER > 11
27
main()28 int main()
29 {
30 #if _LIBCPP_STD_VER > 11
31
32 // Make a couple of sequences
33 using int3 = std::make_integer_sequence<int, 3>; // generates int: 0,1,2
34 using size7 = std::make_integer_sequence<size_t, 7>; // generates size_t: 0,1,2,3,4,5,6
35 using size4 = std::make_index_sequence<4>; // generates size_t: 0,1,2,3
36 using size2 = std::index_sequence_for<int, size_t>; // generates size_t: 0,1
37 using intmix = std::integer_sequence<int, 9, 8, 7, 2>; // generates int: 9,8,7,2
38 using sizemix = std::index_sequence<1, 1, 2, 3, 5>; // generates size_t: 1,1,2,3,5
39
40 // Make sure they're what we expect
41 static_assert ( std::is_same<int3::value_type, int>::value, "int3 type wrong" );
42 static_assert ( int3::size () == 3, "int3 size wrong" );
43
44 static_assert ( std::is_same<size7::value_type, size_t>::value, "size7 type wrong" );
45 static_assert ( size7::size () == 7, "size7 size wrong" );
46
47 static_assert ( std::is_same<size4::value_type, size_t>::value, "size4 type wrong" );
48 static_assert ( size4::size () == 4, "size4 size wrong" );
49
50 static_assert ( std::is_same<size2::value_type, size_t>::value, "size2 type wrong" );
51 static_assert ( size2::size () == 2, "size2 size wrong" );
52
53 static_assert ( std::is_same<intmix::value_type, int>::value, "intmix type wrong" );
54 static_assert ( intmix::size () == 4, "intmix size wrong" );
55
56 static_assert ( std::is_same<sizemix::value_type, size_t>::value, "sizemix type wrong" );
57 static_assert ( sizemix::size () == 5, "sizemix size wrong" );
58
59 auto tup = std::make_tuple ( 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 );
60
61 // Use them
62 auto t3 = extract ( tup, int3() );
63 static_assert ( std::tuple_size<decltype(t3)>::value == int3::size (), "t3 size wrong");
64 assert ( t3 == std::make_tuple ( 10, 11, 12 ));
65
66 auto t7 = extract ( tup, size7 ());
67 static_assert ( std::tuple_size<decltype(t7)>::value == size7::size (), "t7 size wrong");
68 assert ( t7 == std::make_tuple ( 10, 11, 12, 13, 14, 15, 16 ));
69
70 auto t4 = extract ( tup, size4 ());
71 static_assert ( std::tuple_size<decltype(t4)>::value == size4::size (), "t4 size wrong");
72 assert ( t4 == std::make_tuple ( 10, 11, 12, 13 ));
73
74 auto t2 = extract ( tup, size2 ());
75 static_assert ( std::tuple_size<decltype(t2)>::value == size2::size (), "t2 size wrong");
76 assert ( t2 == std::make_tuple ( 10, 11 ));
77
78 auto tintmix = extract ( tup, intmix ());
79 static_assert ( std::tuple_size<decltype(tintmix)>::value == intmix::size (), "tintmix size wrong");
80 assert ( tintmix == std::make_tuple ( 19, 18, 17, 12 ));
81
82 auto tsizemix = extract ( tup, sizemix ());
83 static_assert ( std::tuple_size<decltype(tsizemix)>::value == sizemix::size (), "tsizemix size wrong");
84 assert ( tsizemix == std::make_tuple ( 11, 11, 12, 13, 15 ));
85 #endif // _LIBCPP_STD_VER > 11
86 }
87