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<
for std::string
is a function template. The overloads are:
template<class charT, class traits, class Allocator>
bool operator< (const basic_string<charT,traits,Allocator>& lhs,
const basic_string<charT,traits,Allocator>& rhs);
template<class charT, class traits, class Allocator>
bool operator< (const basic_string<charT,traits,Allocator>& lhs,
const charT* rhs);
template<class charT, class traits, class Allocator>
bool operator< (const charT* lhs,
const basic_string<charT,traits,Allocator>& rhs);
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.
template <class T>
class String
{
};
template <class T>
bool operator< (const String<T>&, const String<T>&) { return true; }
//if a suitable non-template function is available, it can be picked
//bool operator< (const String<char>&, const String<char>&) { return true; }
struct SimpleStruct
{
operator String<char> () { return value; }
String<char> value;
};
int main()
{
String<char> s;
SimpleStruct ss;
s < ss; //the call doesn't match the function template, leaving only the commented-out candidate
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With