operator<
for std::string
是一个函数模板。过载是:
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);
您的调用与任何可用的重载都不匹配,因此它们都将从候选列表中删除。由于没有选择函数模板作为解析调用的候选函数,因此没有任何内容可以将 SimpleStruct 转换为。
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
}