-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP134.CompileTimeIf.cpp
More file actions
77 lines (71 loc) · 1.69 KB
/
P134.CompileTimeIf.cpp
File metadata and controls
77 lines (71 loc) · 1.69 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <tuple>
#include <string>
namespace impl_class_template
{
template<std::size_t N, typename Tuple>
class PrintTuple
{
public:
static void print(std::ostream& os, const Tuple& t)
{
PrintTuple<N-1, Tuple>::print(os, t);
os << ", " << std::get<N-1>(t);
}
};
template<typename Tuple>
class PrintTuple<1, Tuple>
{
public:
static void print(std::ostream& os, const Tuple& t)
{
os << std::get<0>(t);
}
};
template<typename... Args>
std::ostream& operator<<(std::ostream& os, const std::tuple<Args...>& t)
{
os << "(";
PrintTuple<sizeof...(Args), std::tuple<Args...>>::print(os, t);
return os << ")";
}
}
namespace impl_if_constexpr
{
template<std::size_t Index, typename... Args>
std::ostream& printTuple(std::ostream& os, const std::tuple<Args...>& t)
{
if constexpr (Index == 0) // first element
{
os << "(";
}
if constexpr (Index < sizeof...(Args) - 1)
{
os << std::get<Index>(t) << ", ";
return printTuple<Index + 1, Args...>(os, t);
}
else // last element
{
return os << std::get<Index>(t) << ")";
}
}
template<typename... Args>
std::ostream& operator<<(std::ostream& os, const std::tuple<Args...>& t)
{
return printTuple<0, Args...>(os, t);
}
}
int main(int argc, char const *argv[])
{
{
using namespace impl_class_template;
std::tuple<int, std::string, double, std::string> t{1, "hello", 2.0, "world"};
std::cout << t << std::endl;
}
{
using namespace impl_if_constexpr;
std::tuple<int, std::string, double, std::string> t{1, "hello", 2.0, "world"};
std::cout << t << std::endl;
}
return 0;
}