Kleines template-Problem (Spezialisierung)



  • Hallo,

    folgender Code (vereinfacht, inclusive Tippfehler 😉 )

    Header

    template<class Method>
    class Transformation
    {
    public:
    
      template <class X>
      X mapWorldToObject ( const X & obj ) const;
    
    };
    
    // eine Klasse die für template<class Method> verwendet wird
    template<typename T>
    class ConstantCoeffTransformation;
    
    // eine Klasse die für template<class X> verwendet wird
    template<typename T>
    class Sphere;
    

    Wie spezialisiere ich jetzt in der Klasse Transformation die mapWorldToObject-Methode für

    • Method = ConstantCoeffTransformation<T>
    • X = Sphere<T>

    in der Source-Datei.

    Kann mir da jemand weiter helfen?

    Vielen Dank, Thomas



  • Achja, mein Versuch bis jetzt

    template < >
    template < >
    template <typename T>
    Sphere<T> Transformation< ConstantCoeffTransformation<T> >::mapWorldToObject< Sphere<T> > ( const Sphere<T> & obj ) const
    {
    
    }
    


  • Hm... vielleicht ohne die explizite Spezialisierung mit Überladung?

    template <typename T>
    Sphere<T> Transformation< ConstantCoeffTransformation<T> >::mapWorldToObject( const Sphere<T> & obj ) const
    {
    
    }
    


  • Hallo Siassei,

    das sind je im Grunde zwei Spezialisierungen. Eine der Klasse und eine der Methode. Und genauso kann man es auch lösen.

    template<class Method>
    class Transformation
    {
    public:
    
      template <class X>
      X mapWorldToObject ( const X & obj ) const;
    
    };
    
    // eine Klasse die für template<class Method> verwendet wird
    template<typename T>
    class ConstantCoeffTransformation;
    
    // eine Klasse die für template<class X> verwendet wird
    template<typename T>
    class Sphere;
    
    // ---  Spezialisierung der Klasse mit ConstantCoeffTransformation< T >
    template< typename T >
    class Transformation< ConstantCoeffTransformation< T > >
    {
    public:
        template <class X>
        X mapWorldToObject ( const X & obj ) const;
    
        // --   Spezialisierung der Methode mit Sphere< T >
        template<>
        Sphere< T > mapWorldToObject< Sphere< T > >( const Sphere< T > & obj ) const
        {
            // >>> hier Code reinschreiben <<<
            return obj;
        }
    };
    
    template<typename T>
    class ConstantCoeffTransformation
    {
    };
    template<typename T>
    class Sphere
    {
    };
    
    int main()
    {
        using namespace std;
        Transformation< ConstantCoeffTransformation< double > > t;
        Sphere< double > s;
        t.mapWorldToObject( s );
    
        double doof;
        t.mapWorldToObject( doof ); // compiliert, aber linkt nicht, 
                                    //  da mapWorldToObject< double > nicht implementiert ist
        return 0;
    }
    

    Gruß
    Werner



  • Ich würds wie wxSkip machen. Funktionstemplates sollte man allgemein lieber überladen als spezialisieren. Mal davon abgesehen dass partielle Spezialisierungen für Funtkionstemplates (also in deinem Fall mapToWorldObject<Sphere<T> >) in nicht erlaubt sind.


Anmelden zum Antworten