完美模拟nullptr
我厌倦了等待 nullptr
的编译器支持 (gcc 4.6确实如此,但它太新了,很少有发行版支持它)。
因此,作为完全支持 nullptr 之前的权宜之计,我决定模拟它。有两个模拟示例:一个来自此处,另一个来自维基书籍。
值得注意的是,这两个实现都没有提到operator ==
。但是,如果没有它,以下代码将无法编译。
int* ptr = nullptr;
assert( ptr == nullptr ); // error here: missing operator ==
此 operator ==
错误是编译器错误吗?
是否需要更完美的 operator ==
(以及 !=
、<
、<=
等)模拟 nullptr
?
模拟的 nullptr
和真实交易之间还有什么不同?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用 C++0x 编译器编译它,但由于未知原因而失败。它在 C++03 中编译良好。
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.
实际上在您的第一个示例参考的官方提案中提到了这一点:
因此,如果编译器还没有填补这个空白,您应该自己填补这个空白。
It's actually mentioned in the official proposal from your first example reference:
So you should fill this gap yourself if the compiler doesn't yet.