में नेस्टेड क्लास के लिए std :: हैश को विशेषज्ञता देना मेरे पास एक टेम्पलेट क्लास Baz
है जिसमें एक नेस्टेड क्लास Sub
है। मैं std :: हैश विशेषज्ञता के द्वारा इस सबक्लास के लिए हैश फ़ंक्शन को परिभाषित करना चाहता हूं। हालांकि, यह काम नहीं लग रहा है।एक टेम्प्लेट क्लास
#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 >;
}
जीसीसी 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’
अद्यतन
क्या मैं सच में ऐसा करने की कोशिश कर रहा हूँ एक कंटेनर जो स्थिर संदर्भ (सी में नहीं का समर्थन करता है को लागू ++ है भावना) इसके भीतर तत्वों के लिए। मैं उपयोगकर्ता को इन संदर्भों को 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;
};
इस के लिए USECASE क्या है? शायद आपके कंटेनर के लिए एक स्पष्ट हैश फ़ंक्शन निर्दिष्ट करना आसान होगा? –