Boost logo

Boost Users :

Subject: Re: [Boost-users] C++11, boost::filesystem::path, no std::hash specialization
From: Benjamin Kircher (benjamin.kircher_at_[hidden])
Date: 2012-12-05 15:04:09


Hi Nate,

On Sun, Dec 2, 2012 at 11:46 PM, Nathan Crookston <
nathan.crookston_at_[hidden]> wrote:

> Hi Benjamin,
>
> On Sun, Dec 2, 2012 at 1:57 PM, Benjamin Kircher <
> benjamin.kircher_at_[hidden]> wrote:
>
>> I cannot use boost::filesystem::path with an e.g. unordered_set because
>> std::hash seems not to be specialized for this type. Is this intended?
>> Should I file a bug? Or am I completely misguided?
>>
>
> I can't comment on whether filesystem::path should be extended to work
> with boost hash, but I wanted to point out that you could supply your own
> hashing object (not boost hash, but using hash_combine) without needing to
> do anything in the boost namespace. That would work now and in the
> possible future where filesystem::path supports boost::hash.
>
> See <
> http://www.boost.org/doc/libs/1_52_0/doc/html/unordered/hash_equality.html>.
> The patch Daniel James attached to the ticket you referenced is probably a
> good starting point for implementing something like that.
>
>
thanks for the kind reply.

I found out that boost::filesystem already provides a function
hash_value(const path&) which does exactly what you pointed out (it uses
hash_combine/hash_range internally).

I was then able to provide my own specialization of std::hash with:
#include <boost/filesystem.hpp>
#include <boost/functional/hash.hpp>

namespace std
{
    template<> struct hash<boost::filesystem::path>
    {
        size_t operator()(const boost::filesystem::path& p) const
        {
            return boost::filesystem::hash_value(p);
        }
    };
}

Benjamin



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net