How to match that forward declared specialized template function for definition. Its only a min dummy-Class and Hash and shouldn't be of concern here. Thank you for any suggestions that solve my template issue...
#include <iostream>
#include <string>
#include <memory>
class DummyC;
namespace std {
template<> struct std::hash<DummyC> {
std::size_t operator()(std::shared_ptr<DummyC>&) const noexcept; }; }
#include <unordered_map>
struct DummyC{
std::string name;
DummyC(const std::string& n =""):name{n}{}
bool operator ==(const DummyC& s){ return (name==s.name); }
// DummyC():name
};
namespace std {
template<>
std::size_t std::hash<DummyC>::operator()(std::shared_ptr<DummyC> &d) const noexcept{
std::size_t pH = std::hash<unsigned long>{}(reinterpret_cast<unsigned long>(d.get()));
std::size_t nH = std::hash<std::string>{}(d->name);
return nH ^ ((pH%13) & (pH>>2));
}
}
using namespace std;
int main(){
auto t{ make_shared<DummyC>("Dope") };
unordered_map<shared_ptr<DummyC>,int,hash<shared_ptr<DummyC>>> tmap{{t, 50}};
for (const auto& i: tmap) cout<<'|'<<i.first->name<<'>'<<i.second<<"|\n";
}
The error message I'm getting is
hT.cpp:23:34: error: no function template matches function template specialization 'operator()'
std::size_t std::hash<DummyC>::operator()(std::shared_ptr<DummyC> &d) const noexcept{
..so just for the record and anybody looking for similar, thats the cleaned up version thanks to Igor, Paddy and WCraig
#include <iostream>
#include <string>
#include <memory>
struct DummyC;
template<> struct std::hash<std::shared_ptr<DummyC>> {
std::size_t operator()(std::shared_ptr<DummyC> const &) const noexcept; };
#include <unordered_map>
struct DummyC{
std::string name;
DummyC(const std::string& n =""):name{n}{}
bool operator ==(const DummyC& s){ return (name==s.name); }
};
std::size_t std::hash<std::shared_ptr<DummyC>>::operator()(std::shared_ptr<DummyC> const &d) const noexcept{
std::size_t pH = std::hash<void*>{}(reinterpret_cast<void*>(d.get()));
std::size_t nH = std::hash<std::string>{}(d->name);
return nH ^ ((pH%13) & (pH>>2));
}
using namespace std;
int main(){
auto t{ make_shared<DummyC>("Dope") };
unordered_map<shared_ptr<DummyC>,int,hash<shared_ptr<DummyC>>> tmap{{t, 50}};
for (const auto& i: tmap) cout<<'|'<<i.first->name<<'>'<<i.second<<"|\n";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.