Welche der rfind-Überladung wird aufgerufen?
-
Ich bin beim durchschauen der string-Funktionen auf eine Frage gestoßen:
size_type rfind(const E *s, size_type pos = npos) const; size_type rfind(const E *s, size_type pos, size_type n = npos) const;
(Quelle: MSDN)
Wie unterschiedet der Compiler nun, welche der beiden Funktionen ich meine wenn ich z.b.test.rfind(bla.c_str(), 3)
aufrufe? Schließlich treffen doch beide Funktionsaufrufe zu?
-
habe dein beispiel etwas verändert
class E { public: E() {} }; typedef int size_type; const size_type npos = -1; size_type xfind(const E *s, size_type pos = npos) { return 0;} size_type xfind(const E *s, size_type pos, size_type n = npos) { return 0; } int main() { E e; xfind(&e,1); }
g++ mag das nicht
b.cc: In function `int main()': b.cc:15: error: call of overloaded `xfind(E*, int)' is ambiguous b.cc:10: error: candidates are: size_type xfind(const E*, int) b.cc:11: error: size_type xfind(const E*, int, int)
wenn das bei MS funktioniert dann ist das wahrscheinlich eine "microsoft-extension"
Kurt
-
Da ist ein Fehler in der MSDN, der eigentliche Code lautet:
size_type rfind(const E *s, size_type pos = npos) const; size_type rfind(const E *s, size_type pos, size_type n) const; // <- 3 Parameter müssen angegeben werden
-
Ah, ok, dankeschön.