Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am new to expression template programming, I went through this tutorial. It's pretty good I understand things a bit but not able to replicate code to working one. Can someone help ?
What I did -> From [listing 3] I saved it as matrix.h file but don't know how can I make a new matrix object(precisely, I don't know what will be last two params)
Thanks.

What I have tried:

Matrix.h file is form here and In main.cpp I don't how to create matrix. Please any guidance ?
Posted
Updated 20-Mar-18 8:13am

1 solution

The right and left parameters would be of type LeftOp and RightOp and those are of type Matrix. You can see this by looking at the template code in the listing. Here are two snippets :
template< typename T, size_t n, size_t m, typename LeftOp, typename RightOp>
class EtMatrixAdd
{
public:
  EtMatrixAdd(const LeftOp& lhs, const RightOp& rhs) : 
       m_lhs(lhs), m_rhs(rhs) {}
 
  T ElementAt(size_t n, size_t m) const
  {
     return m_lhs.ElementAt(n, m) + m_rhs.ElementAt(n, m);
  }
 
private:
  const LeftOp& m_lhs;
  const RightOp& m_rhs;
};
and
C++
// Replaces operator+ of Listing 2
template<typename T, size_t n, size_t m>
inline EtMatrixAdd<T, n, m, Matrix<T, n, m>, Matrix<T, n, m> >
operator + ( const Matrix<T, n, m>& lhs, const Matrix<T, n, m>& rhs )
{
   return EtMatrixAdd<T, n, m, Matrix<T, n, m>, Matrix<T, n, m>>( lhs, rhs );
}
As you can see in the second snippet, both are of type :
C++
Matrix<T, n, m>;
Where, T is the type of data the matrix holds and n and m are indexes of the items in the matrix that will be added. Look at the first page of the article for examples of declaring the matrix.
 
Share this answer
 
Comments
Member 13737179 20-Mar-18 14:20pm    
so even if I do Matrix<int, 2, 2 >a; Matrix<int, 2, 2 >b; ETMatrixAdd<int 2, 2, a, b>s; it doesn't work , some compilation errors too. I am adding code of both listing-3 and listing-1 in same file matrix.h file. [here](https://ideone.com/XBpkbn)
Rick York 20-Mar-18 15:46pm    
I can't help any more right now but I will try to later, when I am home. :)
Member 13737179 20-Mar-18 15:56pm    
thanks anyways :)
Rick York 20-Mar-18 21:49pm    
After a bit of searching a google, I found this page : http://jeremyko.blogspot.com/2009/08/expression-template.html

Run that through google's translator (if you need to) and you will see an entire article. Hopefully that helps.
Member 13737179 21-Mar-18 1:33am    
Hi Rick, thanks for that link. Will try to extend code to support `*`, `+=`, `*=`

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900