为什么在 C++0x 中没有为 std::weak_ptr 定义 std::hash ?
阅读关于运算符<的讨论后对于 std::weak_ptr,我看不出有任何理由定义 std::hash 以使用 std::weak_ptr 的控制块不起作用。我也不敢相信标准委员会忽视了这一点。有人读过有关该主题的相关讨论吗?
编辑:关于运算符的讨论<对于 std::weak_ptr http://www.open-std.org /jtc1/sc22/wg21/docs/papers/2004/n1590.html
After reading the discussion on operator< for std::weak_ptr, I can't see any reason why defining std::hash to use the control block for std::weak_ptr wouldn't work. I also can't believe that this was ignored by the standards committee. Has anybody read the relevant discussion on the topic?
Edit: Discussion on operator< for std::weak_ptr
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1590.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对这段历史有些了解。您还应该阅读另一篇后来的论文:
http: //www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2637.pdf
我相信后面的这篇论文实际上是当前草案中的内容。总之,shared_ptr 具有基于值的运算符<() 和基于所有权的成员函数owner_before。 weak_ptr 只有后者。
我最好的猜测是对weak_ptr 的哈希支持只是被删除了。据我所知,没有人提出对weak_ptr 进行哈希支持。在将 C++0x 标准化的过程中存在很多很多问题,解决这些问题的时间和资源有限,毫无疑问会出现错误和遗漏。
我还认为要添加对weak_ptr的哈希支持,必须类似于owner_before来完成:将成员函数owner_hash添加到shared_ptr和weak_ptr。我怀疑对 C++0x 来说这样做已经太晚了,但这似乎是一个合理的 TR2 提案。
I am somewhat familiar with the history. There's another later paper you should also read:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2637.pdf
And I believe this later paper is what is actually in the current draft. In summary, shared_ptr has value-based operator<() and ownership-based member functions owner_before. weak_ptr has only the latter.
My best guess is that hash-support for weak_ptr simply got dropped. No one proposed hash support for weak_ptr to the best of my knowledge. There have been many, many issues in bringing C++0x to standardization, a limited amount of time and resources to work the issues, and no doubt there will be mistakes and missing pieces.
I also think to add hash support for weak_ptr it would have to be done analogously to owner_before: Add the member function owner_hash to both shared_ptr and weak_ptr. I suspect it is too late to do this for C++0x, but this seems like a plausible TR2 proposal.
事实证明,日本对此提出了抱怨(参见 lwg 问题 1406),而霍华德在他的答案中所建议的内容正是作为决议提出的。不幸的是,它被推迟了(大概是因为时间限制)。
It turns out that Japan complained about this (see lwg issue 1406), and that exactly what Howard suggests in his answer was proposed as the resolution. Unfortunately, it was postponed (presumably because of time constraints).