1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
// Testing the results
template <std::size_t N, typename TUPLE>
struct TuplePrinter {
std::ostream& operator() (const TUPLE& t, std::ostream& os = std::cout) {
TuplePrinter<N-1, TUPLE>()(t);
os << ", " << std::get<N-1>(t);
return os;
}
};
template <typename TUPLE>
struct TuplePrinter<1, TUPLE> {
std::ostream& operator() (const TUPLE& t, std::ostream& os = std::cout) {
os << std::get<0>(t);
return os;
}
};
template <typename TUPLE>
void printTuple (const TUPLE& t, std::ostream& os = std::cout) {
os << "(";
TuplePrinter<std::tuple_size<TUPLE>::value, TUPLE>()(t);
os << ")" << std::endl;
}
int main() {
using tuple_type = std::tuple<int, std::string, double, char, std::string, int, double, char>;
using reverse_type = reverse_tuple_type<tuple_type>::type;
using alternating_type1 = alternating_tuple_type<tuple_type>::type;
using alternating_type2 = alternating_tuple_type<tuple_type,1>::type;
using alternating_type3 = alternating_tuple_type<tuple_type,1,3>::type;
using alternating_type4 = alternating_tuple_type<tuple_type,2,5>::type;
using rotate_tuple_type1 = rotate_tuple_type<tuple_type, 3>::type;
using rotate_tuple_type2 = rotate_tuple_type<tuple_type, -2>::type;
using rotate_tuple_type3 = rotate_tuple_type<tuple_type, 0>::type;
using rotate_tuple_type4 = rotate_tuple_type<tuple_type, 20>::type;
using tuple_permuted_type = select_subtuple<3,5,4,0,6,2,1,7>::result_type<tuple_type>::type;
using tuple_permuted_some_omitted_type = select_subtuple<3,4,5>::result_type<tuple_type>::type;
const tuple_type t (8, "house", 3.14, 'b', "apple", 6, 1.5, '!');
const reverse_type tuple_reversed = reverse_tuple(t);
const alternating_type1 tuple_alternating1 = alternating_tuple<>()(t);
const alternating_type2 tuple_alternating2 = alternating_tuple<1>()(t);
const alternating_type3 tuple_alternating3 = alternating_tuple<1,3>()(t);
const alternating_type4 tuple_alternating4 = alternating_tuple<2,5>()(t);
const rotate_tuple_type1 tuple_rotated1 = rotate_tuple<3>()(t);
const rotate_tuple_type2 tuple_rotated2 = rotate_tuple<-2>()(t);
const rotate_tuple_type3 tuple_rotated3 = rotate_tuple<0>()(t);
const rotate_tuple_type4 tuple_rotated4 = rotate_tuple<20>()(t);
const tuple_permuted_type tuple_permuted = select_subtuple<3,5,4,0,6,2,1,7>()(t);
const tuple_permuted_some_omitted_type tuple_permuted_some_omitted = select_subtuple<3,4,5>()(t);
std::cout << "original tuple = "; printTuple(t); // (8, house, 3.14, b, apple, 6, 1.5, !)
std::cout << "tuple_reversed = "; printTuple (tuple_reversed); // ('!', 1.5, 6, apple, b, 3.14, house, 8)
std::cout << "tuple_alternating1 = "; printTuple (tuple_alternating1); // (8, 3.14, apple, 1.5)
std::cout << "tuple_alternating2<1> = "; printTuple (tuple_alternating2); // (house, b, 6, !)
std::cout << "tuple_alternating3<1,3> = "; printTuple (tuple_alternating3); // (house, apple, !)
std::cout << "tuple_alternating4<2,5> = "; printTuple (tuple_alternating4); // (3.14, !)
std::cout << "rotate_tuple_type1<3> = "; printTuple (tuple_rotated1); // (b, apple, 6, 1.5, !, 8, house, 3.14)
std::cout << "rotate_tuple_type1<-2> = "; printTuple (tuple_rotated2); // (1.5, !, 8, house, 3.14, b, apple, 6)
std::cout << "rotate_tuple_type1<0> = "; printTuple (tuple_rotated3); // (8, house, 3.14, b, apple, 6, 1.5, !)
std::cout << "rotate_tuple_type1<20> = "; printTuple (tuple_rotated4); // (apple, 6, 1.5, !, 8, house, 3.14, b)
std::cout << "tuple_permuted<3,5,4,0,6,2,1,7> = "; printTuple (tuple_permuted); // (b, 6, apple, 8, 1.5, 3.14, house, !)
std::cout << "tuple_permuted_some_omitted<3,4,5> = "; printTuple (tuple_permuted_some_omitted); // (b, apple, 6)
std::cin.get();
}
|