-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrtp.cpp
More file actions
66 lines (52 loc) · 1.16 KB
/
crtp.cpp
File metadata and controls
66 lines (52 loc) · 1.16 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
#include <iostream>
int multiply(int a, int b){
return a * b;
}
int add(int a, int b){
return a + b;
}
int operate(std::function<int(int, int)> name, int first, int second){
return name(first, second);
}
int operate_1(int(*name)(int, int), int first, int second){
return name(first, second);
}
int operate_ary(int(*name)[5]){
int sum = 0;
for(const auto & x:*name){
sum += x;
}
return sum;
}
auto auto_add(int x, int y){
return x + y;
}
template <typename T, size_t size>
struct S{
S(std::initializer_list<T> args){
int n = 0;
for (T x : args) {buffer[n++]=x;}
}
void print(){
for(int i = 0; i < size; ++i){
std::cout << buffer[i] << ' ';
}
std::cout << '\n';
}
private:
T buffer[size];
};
template <typename... T>
int main(){
std::cout << operate_1(add, 4, 5) << '\n';
std::cout << operate_1(multiply, 4, 5) << '\n';
int ary[5]= {1,2,3,4,5};
std::cout << operate_ary(&ary) << '\n';
auto x5 = { 1, 2, 3 };
std::cout << typeid(x5).name() << '\n';
auto x = auto_add(5, 10);
std::cout << x << '\n';
std::cout << typeid(decltype(x)).name() << '\n';
S<int, 10> s {1,2,3,4,5,6,7,8,9,10};
s.print();
}