我試圖用xercesc :: XMLUri作爲關鍵類型創建一個std :: unordered_map。對於非庫類型,無序的key_type需要散列<>專業化?
#include <unordered_map>
#include "xercesc/util/XMLUri.hpp"
int main()
{
std::unordered_map<xercesc::XMLUri,xercesc::XMLUri> uriMap;
}
導致以下:
clang++ -std=c++11 -O0 -emit-llvm -g3 -Wall -c -fmessage-length=0 -I/usr/include ../xx.cpp
In file included from ../xx.cpp:1:
In file included from /usr/bin/../lib/gcc/i686-linux-gnu/4.7/../../../../include/c++/4.7/unordered_map:43:
/usr/bin/../lib/gcc/i686-linux-gnu/4.7/../../../../include/c++/4.7/bits/functional_hash.h:59:7: error: static_assert failed "std::hash is not specialized for this type"
static_assert(sizeof(_Tp) < 0,
^ ~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/i686-linux-gnu/4.7/../../../../include/c++/4.7/bits/unordered_map.h:45:32: note: in instantiation of template class 'std::hash<xercesc_3_1::XMLUri>' requested here
integral_constant<bool, !__is_final(_Hash)>,
^
/usr/bin/../lib/gcc/i686-linux-gnu/4.7/../../../../include/c++/4.7/bits/unordered_map.h:263:14: note: in instantiation of default argument for '__unordered_map<xercesc_3_1::XMLUri, xercesc_3_1::XMLUri, std::hash<xercesc_3_1::XMLUri>, std::equal_to<xercesc_3_1::XMLUri>, std::allocator<std::pair<const xercesc_3_1::XMLUri, xercesc_3_1::XMLUri> > >' required here
: public __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../xx.cpp:6:54: note: in instantiation of template class 'std::unordered_map<xercesc_3_1::XMLUri, xercesc_3_1::XMLUri, std::hash<xercesc_3_1::XMLUri>, std::equal_to<xercesc_3_1::XMLUri>, std::allocator<std::pair<const xercesc_3_1::XMLUri, xercesc_3_1::XMLUri> > >' requested here
std::unordered_map<xercesc::XMLUri,xercesc::XMLUri> uriMap;
我知道C++ 0x中的無序容器只供應hash<>
特對一些庫類型。如何爲xercesc::XMLUri
創建所需的hash<xercesc::XMLUri>
專業化?
編輯:我想出了這個。這看起來合理嗎?
#include "xercesc\util\XMLUri.hpp"
#include <string>
namespace std
{
size_t hash<xercesc::XMLUri>::operator()(const xercesc::XMLUri& uri) const
{
return hash<std::wstring>()(uri.getUriText());
}
}
這可能是一個無關的問題,但創建一個類模板專業化比使用功能模板專業化的優勢是什麼? – hhbilly
@ user1626720:你別無選擇。 'std :: hash' *是一個類模板。 –
對於'std :: hash'的完整特化應該包含'argument_type'和'result_type'的定義(§20.8.12/ 1)。例如,請參閱我昨天給出的類似問題的答案:http://stackoverflow.com/a/13486174/777186 – jogojapan