Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit conversion and user defined conversion

When I write code like this:

struct foo {
    operator int() const { return 1; }
};
int main() {
    foo a, b;
    auto tmp = (a < b);
}

it works, but while I write code like this:

struct foo {
    operator string() const { return string("foo"); }
};
int main() {
    foo a, b;
    auto tmp = (a < b);
}

Compiler(clang++) says that error: invalid operands to binary expression ('foo' and 'foo')

I wonder why, as both string type and int type have comparison operators, but when foo has a user defined int conversion, it will implicit convert to int to compare, however when foo only has a user defined string conversion, compiler doesn't do implicit conversion though (string)a<(string)b works well.

like image 583
Tsumiki Mikan Avatar asked Feb 20 '17 12:02

Tsumiki Mikan


1 Answers

I think the problem is that string is not a basic type. std::string is a specialization of a template, specifically std::basic_string<char>

So operator < is defined as

template <class CharT, class Traits, class Allocator>
    bool operator< (const std::basic_string<CharT, Traits, Allocator> &_Left,  const std::basic_string<CharT, Traits, Allocator> &_Right);

It will work with:

auto tmp = (static_cast<std::string>(a) < static_cast<std::string>(b));

Then operator < becomes:

bool std::operator< <char, std::char_traits<char>, std::allocator<char>>(const std::string &_Left, const std::string &_Right)
like image 148
JHBonarius Avatar answered Nov 13 '22 21:11

JHBonarius