Specialization std :: hash for a nested class in a template class

I have a template class Bazthat contains a nested class Sub. I would like to define a hash function for this subclass, specializing in std :: hash. However, this does not seem to work.

#include <functional>

struct Foo {
    struct Sub {
    };
};

template <class T>
struct Bar {
};

template <class T>
struct Baz {
    struct Sub {
        int x;
    };
};

// declare hash for Foo::Sub - all right
namespace std {
    template <>
    struct hash< Foo::Sub >;
}

// declare hash for Bar<T> - all right
namespace std {
    template <class T>
    struct hash< Bar<T> >;
}

// declare hash function for Baz<T>::Sub - doesn't work!
namespace std {
    template <class T>
    struct hash< Baz<T>::Sub >;
}

// Adding typename produces a different error.
namespace std {
    template <class T>
    struct hash< typename Baz<T>::Sub >;
}

Gcc 4.5.3 complains:

$ g++ -std=c++0x -c hash.cpp
hash.cpp:34:30: error: type/value mismatch at argument 1 in template parameter list fortemplate<class _Tp> struct std::hash’
hash.cpp:34:30: error:   expected a type, got ‘Baz<T>::Sub’
hash.cpp:40:12: error: template parameters not used in partial specialization:
hash.cpp:40:12: error:         ‘T’

UPDATE

What I'm really trying to do is implement a container that maintains stable references (not in the sense of C ++) to elements inside it. I want to allow the user to embed these links in std::unordered_setand similar, and use them to effectively access or modify existing elements. Below is the layout, not the specific container that I am implementing. The problem is defining a hash function for the reference type.

template <class T>
class Container {
public:
    class Reference {
    public:
        // operator==, operator!=, operator< ...., isNull()
    private:
        size_t index; // index into m_entries (or could be anything else)
        // possibly more stuff
    };

    Reference insert (const T &value);
    Reference find (const T &value);
    void remove (Reference r);
    Reference first ();
    Reference next (Reference prev);

private:
    struct Entry { T value, ... };

    std::vector<Entry> m_entries;
};
+5
3

, , , . , . , :

struct outer1 { typedef int Sub; };
struct outer2 { typedef int Sub; };

, , Sub? . .

, , IFF Sub T. , Sub, .

, . , .

- , metafunction get_hash. typedef "hash_type" . ...

, Sub typedef . .

, , , , , .

+2

Reference .

template <class Container>
class Reference {
public:
    typedef typename Container::value_type value_type; // etc...

    // operator==, operator!=, operator< ...., isNull()
private:
    size_t index; // index into m_entries (or could be anything else)
    // possibly more stuff
};

template <class T>
class Container {
public:
    typedef ::Reference<Container> Reference;
    friend class Reference; // If you cannot help it

    typedef T value_type;

    Reference insert (const T &value);
    Reference find (const T &value);
    void remove (Reference r);
    Reference first ();
    Reference next (Reference prev);

private:
    struct Entry { T value, ... };

    std::vector<Entry> m_entries;
};

:

namespace std {
    template <typename Container>
    struct hash<Reference<Container>>;
}
+4

, . - .

struct yes {};

template <typename T>
yes accept_baz_sub(typename Baz<T>::Sub&&);
void accept_baz_sub(...);

template <class T>
struct is_baz_sub : std::is_same<decltype(accept_baz_sub(std::declval<T>())), yes>
{};

template <typename BazSub>
using CheckedBazSub = typename std::enable_if<is_baz_sub<BazSub>::value, BazSub>::type;

namespace std {
    template <class BazSub>
    struct hash<CheckedBazSub<BazSub>>;
}

: , [temp.alias] §14.5.7.2 .

Another solution would be to write your own hash function function object and let the container use it (for example, the third template parameter std::unordered_map).

0
source

All Articles