Consider the following piece of code:
struct X { operator std::nullptr_t() const { return nullptr; } };
X x;
assert(x == nullptr);
As far as I can tell, it should work because X is implicitly convertible to std::nullptr_t and hence operator== should perform that implicit conversion to the left argument. This seems to be what happens in GCC and Clang, which happily accept the code. CppInsights seems to confirm that the implicit conversion indeed takes place as expected.
However, this code fails to compile in MSVC with the following error (Godbolt link)
error C2676: binary '==': 'main::X' does not define this operator or a conversion to a type acceptable to the predefined operator
Is this code invalid, implementation specific behavior, or is MSVC wrong for rejecting it?
MSVC is wrong in rejecting the code since the program is well formed as the implicit conversion via the conversion operator can be done for the check x == nullptr.
A bug report for the same has been submitted here.
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