-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariadic_example.cpp
More file actions
86 lines (68 loc) · 1.76 KB
/
variadic_example.cpp
File metadata and controls
86 lines (68 loc) · 1.76 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
78
79
80
81
82
83
84
85
86
#include <tuple>
#include <iostream>
#include <type_traits>
using namespace std;
void write() {
// no argument => nothing to do
}
template<typename Arg1, typename ... Args>
void write(const Arg1 &firstArg, const Args & ... remainingArguments) {
cout << firstArg;
write(remainingArguments ...);
}
void test1() {
write("Here is an int : ", 8, "\nHere is a float : ", 5.9F);
}
/// Tuples
template<size_t I = 0, typename Func, typename ... TupleTypes, typename ... CallArgumentTypes>
typename std::enable_if<I == sizeof... (TupleTypes)>::type
for_each_in_tuple(std::tuple<TupleTypes ...> &tpl, Func func, CallArgumentTypes & ... args)
{
}
template<size_t I = 0, typename Func, typename ... TupleTypes, typename ... CallArgumentTypes>
typename std::enable_if<I < sizeof... (TupleTypes)>::type
for_each_in_tuple(std::tuple<TupleTypes ...> &tpl, Func func, CallArgumentTypes & ... args)
{
func(std::get<I>(tpl), args ...);
for_each_in_tuple<I + 1>(tpl, func, args ...);
}
/**
* Template class containing members given as variadic template list
*/
template<typename ... Args>
struct MyContainerClass {
struct doSomethingFunctor {
template<typename T>
void operator()(T && t) {
t.doSomething();
}
};
/**
* Call doSomething() on all the members
*/
void doSomething() {
for_each_in_tuple(m_members, doSomethingFunctor());
}
std::tuple<Args ...> m_members;
};
struct Class1 {
void doSomething() {
cout << "doSomething Class1" << endl;
}
};
struct Class2 {
void doSomething() {
cout << "doSomething Class2" << endl;
}
};
void test2() {
MyContainerClass<Class1, Class2> c;
c.doSomething();
}
int main(int argc, const char * *argv) {
test1();
cout << endl;
test2();
cout << endl;
}
// https://github.com/Pelagicore/ivi-logging/blob/master/include/ivi-logging.h