Re: operator==() of std::string and user-defined conversion
korusef@gmail.com wrote:
What's so special about operator==() for std::string, that it can't be
used in the example below?
#include <iostream>
struct T {}; bool operator==(T const&, T const&) { return true; }
//typedef std::string T; //a == a and e == a fails
I'm not sure I understand. If you define the struct, the
typedef here is illegal. Do you mean that if you use the
typedef instead of the struct, the comparisons mentionned fail
to compile?
struct A
{
operator T() { return T(); }
};
int main(int const argc, char const* argv[])
{
A a = A();
T e = a;
e == e;
e == a;
a == a;
return EXIT_SUCCESS;
}
Supposing the answer to my question above is yes, the answer is
simple: operator== for string is a template function. Which
means that argument deduction is applied to it, and only
instantiations resulting from successful argument deductions are
included in the overload set. Argument deduction generally only
considers a very limited set of conversions: things like array
to pointer, or adding const. User defined conversions are not
considered.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]