1 /* 2 * $Id: aliasing.cc,v 1.2 2004-03-26 07:58:06 opetzold Exp $ 3 * 4 * This example shows the problem with aliasing mentioned at 5 * http://tvmet.sourceforge.net/notes.html#alias 6 */ 7 8 #include <iostream> 9 #include <algorithm> 10 #include <tvmet/Matrix.h> 11 #include <tvmet/util/Incrementor.h> 12 13 using std::cout; using std::endl; 14 using namespace tvmet; 15 16 typedef Matrix<double,3,3> matrix_type; 17 main()18int main() 19 { 20 matrix_type A, B; 21 matrix_type C; 22 23 std::generate(A.begin(), A.end(), 24 tvmet::util::Incrementor<matrix_type::value_type>()); 25 std::generate(B.begin(), B.end(), 26 tvmet::util::Incrementor<matrix_type::value_type>()); 27 28 cout << "A = " << A << endl; 29 cout << "B = " << B << endl; 30 31 // matrix prod without aliasing 32 C = A * B; 33 cout << "C = A * B = " << C << endl; 34 35 // work around for aliasing 36 matrix_type temp_A(A); 37 A = temp_A * B; 38 cout << "matrix_type temp_A(A);\n" 39 << "A = temp_A * B = " << A << endl; 40 41 // this shows the aliasing problem 42 A = A * B; 43 cout << "A = A * B = " << A << endl; 44 } 45