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