Search code examples
c++11hashweak-ptrstdhash

Why was std::hash not defined for std::weak_ptr in C++0x?


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


Solution

  • 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.