I got tired of waiting for compiler support of nullptr
(gcc 4.6 does but it's so new few distributions support it).
So as a stop gap until nullptr
is fully supported I decided to emulate it. There are two examples of emulation: one from here, and one from wikibooks.
Of note, neither implementation mentions an operator ==
. However, without one, the following code will not compile.
int* ptr = nullptr;
assert( ptr == nullptr ); // error here: missing operator ==
Is this operator ==
error a compiler bug?
Is operator ==
(and !=
, <
, <=
, etc) needed to more perfectly emulate nullptr
?
What else is different between an emulated nullptr
and the real deal?
You compiled it with C++0x compiler that failed for unknown reason. It compiles fine in C++03.
Yes, you should implement such a thing. I am, however, surprised that the implicit conversion operators aren't kicking in and allowing you to compare without providing an explicit operator.
template<typename T> bool operator==(T* ptr, nullptr_t null) {
return ptr == 0;
}
template<typename C, typename R> bool operator==(R C::* ptr, nullptr_t null) {
return ptr == 0;
}
// And the reverse
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