Boost logo

Glas :

Re: [glas] arithmetic operations

From: Toon Knapen (toon.knapen_at_[hidden])
Date: 2005-10-27 16:01:54


What if as a user you can configure himself what operator* means?

It will always be controversial to assign a specific behaviour to an
object/symbol. The expected behaviour is always going to be different
for different people with different backgrounds.

Looking specifically at operator overloading. Operator overloading is
necessary if we would like to pass glas-objects to existing algorithms
, algorithms that rely on these specific operators. But since we're also
developing the algorithms in glas, this is not really an issue. OTOH we
might reuse other generic algorithms out there, like those in the STL
but those algorithms allow to pass a functor to specify the operation
(e.g. inner_prod, accumulate). So in our case, operator overloading is
just about 'notation'.

So what if we give the different products clearly distinct names (like
el_prod, inner_prod, ...) ?

Additionally, if a user wants operator* to do an inner_prod, he can just
 define operator* to default to inner_prod. OTOH if the user wants
opeator* to default to el_prod, he just needs to define (via an include
or by importing the operator* into the glas namespace) the free
operator* function. Would'nt that suit all???

(I attached a small example of such an example operator*)

-- 
Toon Knapen
Skype Name: toonknapen
------------------------------------------------
Check out our training program on acoustics
and register on-line at http://www.fft.be/?id=35

#include <iostream>

struct Vector
{
  double dot(const Vector& that) const
  {
    std::cout << "dot" << std::endl ;
    double result = 0 ;
    for(int i = 0 ; i < 3 ; ++i ) result = values_[i] * that.values_[i] ;
    return result ;
  }

  void el_prod(const Vector& that)
  {
    std::cout << "el_prod" << std::endl ;
    for(int i = 0 ; i < 3 ; ++i ) values_[i] *= that.values_[i] ;
  }

  double values_[3] ;
} ;

#if 0
void operator*(Vector& first, Vector& second) { first.dot( second ) ; }
#else
void operator*(Vector& first, Vector& second) { first.el_prod( second ) ; }
#endif

int main()
{
  Vector a, b ;
  a * b ;

  return 0 ;
}