Edit: I have reformatted the post to be clearer.
Why does this work:
struct A {};
struct B {
B(A){}
};
void operator+(const B&, const B&) {}
int main()
{
A a1, a2;
a1 + a2;
}
and this does not?
struct B {
B(const char*){}
};
void operator+(const B&, const B&) {} //error: invalid operands of types 'const char [6]' and 'const char [6]' to binary 'operator+'|
int main()
{
"Hello" + "world";
}
Essentially, in the first example a1
and a2
both convert to B
objects through the implicit conversion and use the operator+(const B&, const B&)
to add.
Following from this example, I would have expected "Hello"
and "world"
to convert to B
objects, again through the implicit constructor, and use operator+(const B&, const B&)
to add to each other. Instead there is an error, which indicates the C-style strings do not attempt a user-defined conversion to B
in order to add. Why is this? Is there a fundamental property that prevents this?