完美模拟nullptr

发布于 2024-11-14 07:59:49 字数 879 浏览 7 评论 0原文

我厌倦了等待 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以往的大感动 2024-11-21 07:59:49

您使用 C++0x 编译器编译它,但由于未知原因而失败。它在 C++03 中编译良好。

You compiled it with C++0x compiler that failed for unknown reason. It compiles fine in C++03.

秋风の叶未落 2024-11-21 07:59:49

是的,你应该实施这样的事情。然而,令我惊讶的是,隐式转换运算符没有启动并允许您在不提供显式运算符的情况下进行比较。

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

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
执手闯天涯 2024-11-21 07:59:49

实际上在您的第一个示例参考的官方提案中提到了这一点:

对几种流行的实验
现有的编译器表明
产生不良和/或误导性的结果
几个的编译器诊断
中描述的常见用例
第 2 节(示例包括:“不
从“const”到“int”的转换;
“没有合适的从‘const class’到‘int’的转换函数
存在”; “模板参数不能引用未命名类型”; “不
运算符“==”匹配这些操作数,操作数类型为: int == const class
”。)我们认为编译器仍然需要添加
nullptr in order的特殊知识
提供质量诊断
常见用例。

因此,如果编译器还没有填补这个空白,您应该自己填补这个空白。

It's actually mentioned in the official proposal from your first example reference:

Experiments with several popular
existing compilers show that it
generates poor and/or misleading
compiler diagnostics for several of
the common use cases described in
section 2. (Examples include: “no
conversion from „const ‟ to „int‟”;
“no suitable conversion function from „const class ‟ to „int‟
exists”; “a template argument may not reference an unnamed type”; “no
operator „==‟ matches these operands, operand types are: int == const class
”.) We believe that compilers will still need to add
special knowledge of nullptr in order
to provide quality diagnos- tics for
common use cases.

So you should fill this gap yourself if the compiler doesn't yet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文