Consider the following code.
#include <iostream>
#include <string>
struct SimpleStruct
{
operator std::string () { return value; }
std::string value;
};
int main ()
{
std::string s; // An empty string.
SimpleStruct x; // x.value constructed as an empty string.
bool less = s < x; // Error here.
return 0;
}
This code does not compile either on g++ or Microsoft Visual C++. The error report given by compilers is no match for operator '<' in 's < x'. The question is why does the compiler not simply convert the SimpleStruct x to string according to the given operator string () and then use operator < ( string, string )?
operator<forstd::stringis a function template. The overloads are:Your call doesn’t match any of the available overloads, so they are all removed from a list of candidates. Since no function template was picked as a candidate for resolving the call, there is nothing to convert SimpleStruct to.