-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_hackery.cpp
54 lines (47 loc) · 1.28 KB
/
template_hackery.cpp
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
/*
* Playing around with fairly trivial recursive templates, perfect-forwarding
* and printing tuples.
*/
#include <tuple>
#include <iostream>
#include <type_traits>
template<long l>
struct factorial : std::integral_constant<long, l * factorial<l - 1>()> {};
template<>
struct factorial<1> : std::integral_constant<long, 1> {};
template<typename ... T>
auto tuple_factory(T&& ... t)
{
return std::make_tuple(std::forward<T>(t)...);
}
template<size_t I, typename... T>
struct tuple_printer
{
static void print(std::ostream& out, std::tuple<T...> t)
{
tuple_printer<I - 1, T...>::print(out, t);
out << ", " << std::get<I - 1>(t);
}
};
template<typename... T>
struct tuple_printer<1, T...>
{
static void print(std::ostream& out, std::tuple<T...> t)
{
out << std::get<0>(t);
}
};
template<typename ... T>
auto& operator<<(std::ostream& os, std::tuple<T...> t)
{
os << "(";
tuple_printer<sizeof...(T), T...>::print(os, t);
os << ")";
return os;
}
int main() {
auto t = tuple_factory(42, 3.14, "Hello!", factorial<6>());
std::tuple<int, float, std::string> mytup = {2, 3.1, "Hello!"};
std::cout << t << std::endl;
static_assert(std::is_integral<factorial<1>::value_type>(), "factorial value type is not an integral type");
}