-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathnontypeparam.cpp
67 lines (56 loc) · 1.6 KB
/
nontypeparam.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
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
using namespace std;
// Class Template
template<typename T, size_t rows, size_t cols>
class Mat
{
T data[rows][cols];
public:
Mat(){}
//// the default copy constructor will copy each element of a static array member
//// so we do not 'delete' the copy constructor
//// the same with the assignment operator
// Mat(const Mat&) = delete;
// Mat& operator=(const Mat&) = delete;
T getElement(size_t r, size_t c);
bool setElement(size_t r, size_t c, T value);
};
template<typename T, size_t rows, size_t cols>
T Mat<T, rows, cols>::getElement(size_t r, size_t c)
{
if ( r >= rows || c >= cols)
{
cerr << "getElement(): indices are out of range" << endl;
return 0;
}
return data[r][c];
}
template<typename T, size_t rows, size_t cols>
bool Mat<T, rows, cols>::setElement(size_t r, size_t c, T value)
{
if ( r >= rows || c >= cols)
{
cerr << "setElement(): Indices are out of range" << endl;
return false;
}
data[r][c] = value;
return true;
}
template class Mat<int, 2, 2>; // Explicitly instantiate template Mat<int, 2, 2>
typedef Mat<int, 2, 2> Mat22i;
//template Mat<float, 3, 1> will be instantiate implicitly
int main()
{
Mat22i mat;
mat.setElement(2, 3, 256);
cout << mat.getElement(2, 3) << endl;
mat.setElement(1, 1, 256);
cout << mat.getElement(1, 1) << endl;
Mat<float, 3, 1> vec;
vec.setElement(2, 0, 3.14159f);
cout << vec.getElement(2, 0) << endl;
Mat<float, 3, 1> vec2(vec);
cout << vec2.getElement(2, 0) << endl;
// vec2 = mat; //error
return 0;
}