Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSVC won't perform a user-defined implicit conversion to std::nullptr_t when invoking operator==

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?

like image 815
Daniel Avatar asked Jun 06 '26 20:06

Daniel


1 Answers

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.

like image 60
Anoop Rana Avatar answered Jun 09 '26 10:06

Anoop Rana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!