11.07.2015 Views

Understanding C++ Expression Templates - Angelika Langer

Understanding C++ Expression Templates - Angelika Langer

Understanding C++ Expression Templates - Angelika Langer

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

15A Simplified OO ImplementationEliminate the representation of the composite and the leafobject as data members.! Instead of passing information to the constructor andmemorizing them for subsequent evaluation,! pass them to the directly to the evaluation function.SimpleDotProduct (T* a, T* b) :v1(a), v2(b) {}virtual T eval() { return (*v1)*(*v2); }becomesT eval(T* a, T* b, size_t dim) { return (*a)*(*b); }© Copyright 1995-2000 by <strong>Angelika</strong> <strong>Langer</strong>. All Rights Reserved.http://www.<strong>Angelika</strong><strong>Langer</strong>.com(29)last update: 11/7/2005 ,21:37A Simplified OO Implementationtemplate class CompositeDotProduct : public DotProduct {public:virtual T eval(T* a, T* b, size_t dim){ return SimpleDotProduct().eval(a,b,dim)+ ((dim==1) ? 0: CompositeDotProduct().eval(a+1,b+1,dim-1));}};template class SimpleDotProduct : public DotProduct {public:virtual T eval(T* a, T* b, size_t dim){ return (*a)*(*b); }};© Copyright 1995-2000 by <strong>Angelika</strong> <strong>Langer</strong>. All Rights Reserved.http://www.<strong>Angelika</strong><strong>Langer</strong>.com(30)last update: 11/7/2005 ,21:37

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!