-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctors_templates.cpp
More file actions
76 lines (56 loc) · 1.43 KB
/
functors_templates.cpp
File metadata and controls
76 lines (56 loc) · 1.43 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
// Test.cpp : Defines the entry point for the console application.
//
// This is my test code
// This repo will be used to write test codes.
#include "stdafx.h"
#include <iostream>
#include <memory>
class power
{
private:
int pow;
power() {};
public:
power(int k) :pow(k)
{
}
int operator () (int x)const
{
int ret = x;
for (int i = 0; i < pow; i++)
{
ret *= x;
}
return ret;
}
};
using namespace std;
template <typename T> T abs(T t)
{
if (t < 0)
{
return -t;
}
return t;
};
int main1()
{
power p2(2);
power p3(3);
unique_ptr<int> pint(new int(4));
auto p = move(pint);
cout << " 4 to the power 3 (with unique_ptr) --> 4^3 = " << p3(*(p)) << endl;
cout << " 6 to the power 3 --> 6^3 = " << p3(6) << endl;
cout << " 3 to the power 3 --> 3^3 = " << p3(3) << endl;
cout << " new template function" << abs<int>(-5) << endl;
cout << " new template function" << abs<short>(-11) << endl;
cout << " new template function" << abs<long>(-999) << endl;
cout << " new template function" << abs<float>(-12.343) << endl;
cout << " new template function" << abs<double>(-764.386) << endl;
cout << " new template function" << abs<int>(5) << endl;
cout << " new template function" << abs<short>(11) << endl;
cout << " new template function" << abs<long>(999) << endl;
cout << " new template function" << abs<float>(12.343) << endl;
cout << " new template function" << abs<double>(764.386) << endl;
return 0;
}