//clang /* A simple matrix package to illustrate some C++11 features * * Everything is in the same file to make it simpler for the lecture. * However, clearly this is not good coding practice. * Furthermore, I've placed "using namespace std" at the top, and * you definitely don't want to do this in a header file */ #include #include #include using namespace std; template class simpmat { public: D &operator()(int i, int j) { return data[i][j]; } D operator()(int i, int j) const { return data[i][j]; } D max() const { auto first = true; D ret; for(auto i : data) for(auto j : i) if (first || ret,M> data; }; template auto operator*(const simpmat &x1, const simpmat &x2) -> simpmat { // an MxN matrix times a NxP matrix produces a MxP matrix // code placed here for simplicity // not the most efficient algorithm for cache hits, etc. // just for illustration! typedef decltype(x1(0,0)*x2(0,0) + x1(0,0)*x2(0,0)) Dret; simpmat ret; for(int i=0;i void printmat(const simpmat &m, ostream &os) { for(int i=0;i0;--i) ret += s; return ret; } simpmat<1,1,double> operator"" _mat(long double val) { simpmat<1,1,double> ret; ret(0,0) = val; return ret; } int main(int arg, char **argv) { auto m = 4.5_mat; auto m2 = 2.0_mat; printmat(m*m2,cout); return 0; }