템플릿 클래스 Baz
에 중첩 클래스 Sub
이 있습니다. std :: hash를 전문화하여이 서브 클래스의 해시 함수를 정의하고 싶습니다. 그러나, 그것은 작동하지 않는 것 같습니다.템플릿 클래스의 중첩 클래스에 대한 std :: hash 특수화
#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 불평 :
$ g++ -std=c++0x -c hash.cpp
hash.cpp:34:30: error: type/value mismatch at argument 1 in template parameter list for ‘template<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
내가 정말 할 노력하고있어 것이 아니라 C 안정 참조를 (지원하는 컨테이너를 구현하는 것입니다 ++ 감각)을 그 안에있는 요소들에 적용합니다. 사용자가 이러한 참조를 std::unordered_set
및 그 유사 항목에 삽입하고 기존 요소를 효율적으로 액세스하거나 수정하는 데 사용하도록 허용하려고합니다. 다음은 실물 모형입니다. 구현중인 컨테이너가 아닙니다. 문제는 참조 유형에 대한 해시 함수를 정의하는 데 있습니다.
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;
};
이의 쓰임새는 무엇입니까? 아마도 컨테이너에 명시적인 해시 함수를 지정하는 것이 더 쉬울까요? –