partielle template Spezialisierung?
-
Ach herje ... das hab ich mir anders vorgestellt

Wie mache ich das am geschicktesten?! Die Klasse erben? Wie weise ich dann aber nur den Wert zu?!
template<class T, const int N> struct base { static T value; }; template<const int N> struct deri : public base<float, N> { static float value; // Das kann ich doch nicht einfach "überladen", da ja dann value 2x existiert, oder? };
-
Spezialisier doch einfach die ganze Klasse..
template <class T, class N> class tcon { static T RANGE; }; template<class T,class N> T tcon<T,N>::RANGE = 0; template<class N> class tcon<float,N>{ static float RANGE; }; template<class N> float tcon<float,N>::RANGE = 1.234f;
-
drakon schrieb:
Spezialisier doch einfach die ganze Klasse..
template <class T, class N> class tcon { static T RANGE; }; template<class T,class N> T tcon<T,N>::RANGE = 0; template<class N> class tcon<float,N>{ static float RANGE; }; template<class N> float tcon<float,N>::RANGE = 1.234f;Dann muss ich sämtliche methoden und variablen von tcon in die tcon<float,N> kopieren oder?
-
Wie ich das gerade sehe. Ja, sofern sie abhängig vom Template Paramter sind. Die anderen kannst du gut in einer Basisklasse haben und dann davon ableiten.
-
drakon schrieb:
Wie ich das gerade sehe. Ja, sofern sie abhängig vom Template Paramter sind. Die anderen kannst du gut in einer Basisklasse haben und dann davon ableiten.
Jetzt sitz ich völlig auf der Leitung! Wie kann ich denn da ableiten?!
template <class T, class N> class tcon { static T RANGE; public: void sort(); }; template<class T,class N> T tcon<T,N>::RANGE = 0; template<class N> class tcon<float,N> : public tcon<float,N> //? Der Compiler streikt da .... { static float RANGE; }; template<class N> float tcon<float,N>::RANGE = 1.234f; int main() { tcon<float,23> t; t.sort(); }//Edit: Es ist so gut wie alles float T abhängig!

-
lager doch die bestimmung der variable einfach in ne andere Klasse aus:
template<class T,class N> class tconhelper { protected: static T RANGE; }; template<class T,class N> T tconhelper<T,N>::RANGE = 0; template<class N> struct tconhelper<float,N> { static float RANGE; }; template<class N> float tconhelper<float,N>::RANGE = 1.234f; template<class T,class N> class tcon:public tconhelper<T,N> { typedef tconhelper<T,N> Helper; void test() { Helper::RANGE=3;//das ist der Preis den man hier zahlen muss ;) } };
-
//Edit: Es ist so gut wie alles float T abhängig!
Im übrigen solltest du dir nochmal Gedanken machen. Wenn eh alles praktisch alles float ist. Warum dann ein template?! Da reicht doch eine normale Klasse.
-
drakon schrieb:
//Edit: Es ist so gut wie alles float T abhängig!
Im übrigen solltest du dir nochmal Gedanken machen. Wenn eh alles praktisch alles float ist. Warum dann ein template?! Da reicht doch eine normale Klasse.
Nicht von float, ich meinte T. Nochmal ganz konkret:
template<class T, const int N> class tcon { T reichtum; T schoenheit; static T RANGE; :rolling_eyes: public: void beherrscheWelt(); void gehZuBett(); T sinnDesLebens(); }; template<class T, const int N> T tcon<T,N>::RANGE = 0;Ich möchte nun das alles völlig identisch ist wenn ich für T float wähle bis auf die Tatsache, dass der Initialwert der statischen Variable nicht 0 sondern 1,234 ist
Jedoch unabhängig von N

Ich spüre die Lösung kommen!

-
Was macht eigentlich das N. Dir ist klar das für jedes Objekt dann ne neue Klasse erzeugt werden könnte -> tcon<int,4> - tcon<int,23> - tcon<int,342> -> 3 verschieden Klassen, wo es eine vllt auch getan hätte.
Vielleicht wäre es hier angebracht das N als Ctor Parameter zu nehmen, jenachdem was du damit überhaupt genau vor hast ...
-
Ansonsten ist das von otze die gängiste Lösung. Wenn T nur das "Verhalten" von RANGE bestimmt und sich nicht auf die anderen Daten "so drastisch" auswirkt, dann wäre es wirklich angebracht RANGE da raus zu holen ...
-
Otze's Beitrag hab ich irgendwie unter den Tisch fallen lassen
! So mache ichs jetzt und es funktioniert hervorragend! Vielen Dank an alle
