Topic: operators for builtin classes


Author: marc@offline.be (Marc Duponcheel)
Date: 10 Sep 93 00:55:41 PST
Raw View
It is allowed in C++ to apply a dtor to an object of a builtin class (e.g. int)

 int i;
 i.~int(); // apply dtor, understood by compiler

This is very handy when one uses templates like

 template <class T>
 void Destruct(T& t)
 {
  t.~T(); // becomes t.~int() if T is int
 }

 Destruct(i);

========
QUESTION
========
I would like to know if it would be a good idea to extend this to builtin operators too
(e.g int& int::operator +=(const int&))


int i;
int j;

i = i.operator += (j);

This way one could use the algebra of a template parameter T (e.g. int) to make a similar
algebra for say AlgVec<T> and one could do this by using an Apply which
factors out the typival for(int idx = 0; idx < Size(); ++idx) loop using pointer
to member functions like:

template <class T>
class AlgVec : public Vector<T>
{
protected:
 AlgVec<T>& Apply(T& (T::*asgnop)(const T&), const AlgVec<T>& avc);
public:

 AlgVec<T>& operator += (const AlgVec<T>& avc)
 {
  return Apply(&T::operator +=, avc);  // becomes int::operator += if T is int
 }
// similar for -= *= etc ...
};

template <class T>
AlgVec<T>& AlgVec<T>::Apply(T& (T::*asgnop)(const T&), const AlgVec<T>& avc)
{
 if(Size() == avc.Size())
  for(int idx = 0; idx < Size(); ++idx)
   ((*this)[idx].*asgnop)(avc[idx]) // apply whatever assignment
 else
 {
  // don't know ... it is only an example :-)
 }
 return *this;
}

I know one can specialize AlgVec<int>& AlgVec<int>::operator += (const AlgVec<int>& avc)
but this needs to be done for all builtin types. Can't the compiler do this for me ?

Moreover, one typically uses objetcs of calss AlgVec<T> where T is a builtin class
and one typically wants to do algebraic operations on such objects.

 -- marc.

################################################################################
 email  preferred address    marc@offline.be [= me@home]
           aka               marc@offline.UUCP ub4b!offline!marc offline!marc
        if [me@home] fails   mdu@abacus.be [= me@brussels.work]
           or                mdp@cimad.be  [= me@antwerp.work]
 fido   2:292/603.26  Marc.Duponcheel@p26.f603.n292.z2.FidoNet.Org [= me@home]
 bix    mduponcheel   mduponcheel@BIX.com [= me@home]
################################################################################