Partial specialization of template function



  • How to make it?

    This does not works:

    template<int N, class T> inline T CalculateDeterminant(cMatrix<N,N,T> &m)
    {
        [ The code that calls CalculateDeterminant(cMatrix<N-1,N-1,T>) ]
    }
    
    template<class T> inline T CalculateDeterminant<1>(cMatrix<1,1,T> &m)
    {
        return m.FirstVector().FirstElement();
    }
    


  • template<1, class T> inline T CalculateDeterminant<1>(cMatrix<1,1,T> &m)
    {
    return m.FirstVector().FirstElement();
    }



  • try_this, it does not works too.

    I used function overload instead:

    template<int N, class T> inline T CalculateDeterminant(cMatrix<N,N,T> &m)
    {
        [ The code that calls CalculateDeterminant(cMatrix<N-1,N-1,T>) ]
    }
    
    template<class T> inline T CalculateDeterminant(cMatrix<1,1,T> &m)
    {
        return m.FirstVector().FirstElement();
    }
    

    What is the difference between overload and template specialization?



  • try this:
    [cpp]
    template<class T> inline T CalculateDeterminant<1**,T**>(cMatrix<1,1,T> &m)
    {
    return m.FirstVector().FirstElement();
    }[/cpp]

    What is the difference between overload and template specialization?
    Well overloading is just that overloading (same method but different parameter list) and specialization is a version of a template function which is more restrictive concerning its template parameters.



  • Ahem, guys, you do know that function templates cannot be specialized partially, do you? Neither try_this' nor lolz' solution are allowed in standard C++. The only thing you can do is a full template specialization - and you better do not, because the specialization does not participate in overload resolution. Use overloading instead.

    The overloading SAn uses is the right way.


  • Mod

    pumuckl schrieb:

    The only thing you can do is a full template specialization - and you better do not, because the specialization does not participate in overload resolution.

    Which may actually be what's intended. Anyway, it is always possible to defer a function template to a class template und thus gain the benefit of partial specializations. Depending on the complexity this can be preferable to overloading since overloading does more easily (and subtly) result in ambiguities.



  • What does it mean: “the specialization does not participate in overload resolution” ?



  • camper schrieb:

    ... it is always possible to defer a function template to a class template und thus gain the benefit of partial specializations. ...

    Hm... Is it possible to make static operator() in the class, so I can “call class like function”?


  • Mod

    SAn schrieb:

    camper schrieb:

    ... it is always possible to defer a function template to a class template und thus gain the benefit of partial specializations. ...

    Hm... Is it possible to make static operator() in the class, so I can “call class like function”?

    No, member functions used for operator overloading must always be non-static (except for new/new[]/delete/delete[]). But since it's an implementation detail, there is no disadvantage in using a normal member function (static or not), or that class template could model a function object (which may as well be part of the interface to be used with standard algorithms).



  • template<class T,int N> class Matrix
    {
    public:
      Matrix():ele(N){};
      T ele;
    };
    
    template<class T,int N> T Calc(Matrix<T,N>& r)
    {
      return Calc(Matrix<T,N-1>());  
    };
    
    template<class T> T Calc(Matrix<T,1>& r)
    { 
      return r.ele;
    };
    
    void test()
    {
      Matrix<double,3> m; 
      double ele;
    
      ele=Calc(m);
    }
    

    sometimes it make sense.
    The last call is

    template<class T> T Calc(Matrix<T,1>& r)
    

Anmelden zum Antworten