dimanche 28 février 2016

How to define member class operator based on the class parameters

Is it possible to define different = operators for different template arguments. Let's assume that I want to use different methods for converting arguments of different types:

template <class T,class U>
class cTest
{
  private:
  public:
    T x;
    U y;

  //typical case
  cTest<T,U>& operator =(const cTest<T,U> &that)
  {
     return *this;
  }

  //operator = based on the LHS type 1
  cTest<uint16_t,U>& operator =(const cTest<int,U> &that)
  {
     cout<<"cTest<uint16_t,U>& operator =(const cTest<int,U> &that)"<<endl;
     return cTest<uint16_t,U>();
   }
  //operator = based on the LHS type 2
   cTest<uint8_t,U>& operator =(const cTest<int,U> &that)
   {
      cout<<"cTest<uint8_t,U>& operator =(const cTest<int,U> &that)"<<endl;
      return cTest<uint16_t,U>();
   }
};

Aucun commentaire:

Enregistrer un commentaire