bedingte Template-Operatoren --> Compiler-unabhängig?
-
Hallo zusammen,
durch probieren hab ich unten dargestellte Template-Operator-Kombination hinbekommen. Weiß jemand, ob das nur ein Compiler-abhängiges Verhalten widerspiegelt oder ob das immer so sein muss? Wenn letzteres, warum?
(Knackpunkt: Beim 2. Operator könnte es sich um eine Re-Definition des 1. handeln)Hier Das Beispielprogramm:
#include <iostream>
using namespace std;class AA
{
private:
public:
typedef bool OP_AA ;
int i;
};class BB
{
private:
public:
typedef bool OP_BB ;int i;
};template <class Typ>
typename Typ::OP_AA operator>(const Typ& _s, const Typ& _r)
{
cout<<"In OP_AA "<<endl;
return true;
}template <class Typ>
typename Typ::OP_BB operator>(const Typ& _s, const Typ& _r)
{
cout<<"In OP_BB "<<endl;
return false;
}int main(int argc, char* argv[], char* env[])
{AA aa_x, aa_y;
BB bb_x, bb_y;
bool frage = true;frage = (aa_x > aa_y);
cout<<"frage (aa_x > aa_y) "<<frage<<endl;frage = (bb_x > bb_y);
cout<<"frage (bb_x > bb_y) "<<frage<<endl;return 0;
}Liefert die Ausgabe:
###>
In OP_AA
frage (aa_x > aa_y) 1
In OP_BB
frage (bb_x > bb_y) 0
###>
-
Hallo,
der Code ist in Ordnung. Er nutzt das SFINAE (Substitution Failure is not an error)-Prinzip. Wenn bei der Überladungsauflösung ein oder mehrere Template-Instanziierungen ungültig sind, aber trotzdem eine passende Funktion existiert, so werden die ungültigen Instanziierungen einfach verworfen.In deinem Beispiel:
Für aa_x > aa_y existieren mindestens zwei Überladungen (deine beiden Templates). Die erste wird zu:
AA::OP_AA operator>(const AA& _s, const AA& _r);
die zweite zu:
AA::OP_BB operator>(const AA& _s, const AA& _r);
Da AA keinen Typ namens OP_BB besitzt, wird die zweite Überladung ignoriert.
-
Erst mal vielen Dank an HumeSikkins
Was ich im Zusammenhang mit SFINAE dann aber nicht verstehe ist, warum die folgende Konstruktion nicht klappt (s.u.).
Hier bekomme ich dann die Fehlermeldung (beim 2. Operator-Template mit Bezug auf's erste):
Error 174: "mytest.cpp", line 248 # Function redefinition; previously defined as "bool operator >(const #1 &,const #1 &)" at ["mytest.cpp", line 241]. bool operator>(const Typ& _s, const Typ& _r)Auch Veruche, OP_xx in "aktiveren" Code zu stecken, klappten nicht. Ist das dann ein Kompiler-Fehler oder bezieht sich SFINAE nur auf den Rückgabewert?
class AA { private: public: typedef int OP_AA ; int i; }; class BB { private: public: typedef int OP_BB ; int i; }; template <class Typ> bool operator>(const Typ& _s, const Typ& _r) { return true; //(_r < _s); typename Typ::OP_AA dummy; } template <class Typ> bool operator>(const Typ& _s, const Typ& _r) { return true; //(_r < _s); typename Typ::OP_BB dummy; } int main(int argc, char* argv[], char* env[]) { AA aa_a,aa_b; bool frage = true; frage = (aa_a > aa_b); cout<<"frage "<<frage<<endl; return 0; }
-
Die beiden Funktionen unterscheiden sich nicht, der Prototyp muss sich aber unterscheiden, du hast die Funktion hier nicht überladen, sondern zweimal mit unterschiedlichem Rumpf.
-
Vielen Dank Anon. Ich glaube, dass ich's jetzt gefressen habe --> die wunderbare Welt des C++