template soll element in Struktur adressieren
-
Hallo
ich habe folgendes Problem:
Ich will Funktion ModelCodesEq als ein template umformulieren.Der vector<EtsTypPtr> ist ein Vector von shared_ptr<EtsTyp>.
Das Element typ_modell_code soll hierbei als Templateparameter dienen.struct EtsTyp { explicit EtsTyp(const CRecord &Rec) ; int typ_schwackecode ; std::string typ_vehicle_type ; int typ_make_code ; int typ_modell_code ; std::string typ_name ; std::string typ_edition_line ; int typ_fueltype ; int typ_bodytype ; int typ_transmission ; int typ_geartype ; int typ_gears ; int typ_doors ; int typ_ccm ; int typ_kw ; int typ_cylinder ; int typ_gvw ; int typ_wheelbase ; int typ_net_weight ; int typ_payload ; int typ_seats ; CMonthYear typ_prodbeg ; CMonthYear typ_prodend ; bool InProdTimes(const CMonthYear &RegMonthYear) const ; bool NotInProdTimes(const CMonthYear &RegMonthYear) const ; } ;bool ModelCodesEq(const vector<EtsTypPtr> &FoundedCodes) const { return find_if(FoundedCodes.begin(),FoundedCodes.end(), bind(std::not_equal_to<int(),bind(&EtsTyp::typ_modell_code,_1),(*FoundedCodes.begin())->typ_modell_code ))==FoundedCodes.end() ; }Es soll möglich sein die einzelnen Elemente der Struktur als template Parameter
anzugeben,statt typ_modell_code nun typ_bodytype.
Ich hab keinen Schimmmer wie ich das Ganze formulieren muss.Danke für eure Hilfe.
-
Wie soll ein
intals ein Templateparameter dienen?Beschreib mal genau, was du erreichen willst und nicht wie.
-
Meinst du sowas?
template<int EtsTyp::* code> bool ModelCodesEq(const vector<EtsTypPtr> &FoundedCodes) const { return find_if(FoundedCodes.begin(),FoundedCodes.end(), bind(std::not_equal_to<int(),bind(code,_1),(*FoundedCodes.begin())->*code ))==FoundedCodes.end() ; } //Aufruf: if(ModelCodesEq<&EtsTyp::typ_modell_code>(irgendein_vector)) ...;Keine Ahnung, ob das so funktioniert, aber falls, könntest du diesen
int EtsTyp::* codeauch als normalen Parameter übergeben, ich sehe jetzt nicht unbedingt, warum das ein Templateparameter sein muss, das macht das ganze imho nur unleserlich.
-
ipsec schrieb:
ich sehe jetzt nicht unbedingt, warum das ein Templateparameter sein muss, das macht das ganze imho nur unleserlich.
Evtl. sols ein Template werden weil es nicht immer um einen int geht. Das wäre dann in etwas sowas:
template<class T> bool ModelCodesEq(const vector<EtsTypPtr> &FoundedCodes, T EtsTyp::* memPtr) const { return find_if(FoundedCodes.begin(), FoundedCodes.end(), bind(std::not_equal_to<T>(),bind(memPtr,_1), (*FoundedCodes.begin())->*memPtr ) ) == FoundedCodes.end() ; } //Aufruf: if(ModelCodesEq(irgendein_vector, &EtsTyp::typ_modell_code)) ...;
-
Hallo,
pumuckl hat mein Problem gelöst.
Genau das hab ich gesucht.
Ich habe das Ganze zwar schon mit einem Predicate gelöst, aber pumuckls Lösung
sieht um einiges besser aus.Danke an euch alle.