1 Funktionstemplate für >1 Klassentemplates?



  • Ich habe ein paar Klassentemplates, für die ich ein paar Operatoren global überladen will. Die Klassentemplates haben alle so die Form

    template<class T>
    class MyClassX
    {
    public:
    	// ... die Details ...
    	MyClassX<T> & operator+=(T const & other){data += other; return *this;};
    	T data;
    };
    

    .

    Der globale operator+ sollte so aussehen

    template<class T>
    MyClassX<T> operator+(MyClassX<T> const & a,T const & b){return MyClassX<T>(a) += b;};
    

    .

    Nun habe ich wie gesagt nicht nur ein solches Klassentemplate, sondern mehrere, für die ich operator+ genau gleich implementieren will. Ich würde das gern mit einem Template (für alle Klassentemplates) lösen, aber wie soll ich das schreiben?


  • Mod

    müsste so gehen?

    template<class T,template class<class> TT>
    TT<T> operator+(TT<T> const & a,T const & b){return TT<T>(a) += b;};
    


  • camper schrieb:

    müsste so gehen?

    template<class T,template class<class> TT>
    TT<T> operator+(TT<T> const & a,T const & b){return TT<T>(a) += b;};
    

    Syntaktisch korrekt wäre:

    template<class T,template <class> class TT> 
    TT<T> operator+(TT<T> const & a,T const & b){return TT<T>(a) += b;}
    

  • Mod

    stimmt, wär mir aufgefallen, hab aber grad keinen compiler zur hand. hauptsache das konzept kommt rüber 🙂



  • Danke.


Anmelden zum Antworten