我在this问题this实施中找到hash
tuple
的通用unordered_map
在VariadicTuple.cpp
中使用。
以下是我在#include <tuple>
//code taken from https://stackoverflow.com/questions/7110301/generic-hash-for-tuples-in-unordered-map-unordered-set
namespace std {
namespace
{
// Code from boost
// Reciprocal of the golden ratio helps spread entropy
// and handles duplicates.
// See Mike Seymour in magic-numbers-in-boosthash-combine:
// https://stackoverflow.com/questions/4948780
template <class T>
inline void hash_combine(std::size_t& seed, T const& v)
{
seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
// Recursive template code derived from Matthieu M.
template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
struct HashValueImpl
{
static void apply(size_t& seed, Tuple const& tuple)
{
HashValueImpl<Tuple, Index - 1>::apply(seed, tuple);
hash_combine(seed, std::get<Index>(tuple));
}
};
template <class Tuple>
struct HashValueImpl<Tuple, 0>
{
static void apply(size_t& seed, Tuple const& tuple)
{
hash_combine(seed, std::get<0>(tuple));
}
};
}
template <typename ... TT>
struct hash<std::tuple<TT...>>
{
size_t
operator()(std::tuple<TT...> const& tt) const
{
size_t seed = 0;
HashValueImpl<std::tuple<TT...> >::apply(seed, tt);
return seed;
}
};
}
中保存的链接问题中复制并粘贴的代码:
1>------ Build started: Project: CloudCache, Configuration: Debug Win32 ------
1> VariadicTuple.cpp
1> Utility.cpp
1> Memoization.cpp
1> HelloWorld.cpp
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\xstddef(381): error C2338: The C++ Standard doesn't provide a hash for this type.
1> c:\users\llovagnini\source\repos\cloudcache\cloudcache\cloudcache\variadictuple.cpp(18): note: see reference to class template instantiation 'std::hash<T>' being compiled
1> with
1> [
1> T=std::vector<double,std::allocator<double>>
1> ]
1> c:\users\llovagnini\source\repos\cloudcache\cloudcache\cloudcache\variadictuple.cpp(37): note: see reference to function template instantiation 'void std::`anonymous-namespace'::hash_combine<std::vector<double,std::allocator<_Ty>>>(size_t &,const T &)' being compiled
1> with
1> [
1> _Ty=double,
1> T=std::vector<double,std::allocator<double>>
1> ]
1> c:\users\llovagnini\source\repos\cloudcache\cloudcache\cloudcache\variadictuple.cpp(36): note: while compiling class template member function 'void std::`anonymous-namespace'::HashValueImpl<std::tuple<_Ty>,0>::apply(size_t &,const Tuple &)'
1> with
1> [
1> _Ty=vecD,
1> Tuple=std::tuple<vecD>
1> ]
1> c:\users\llovagnini\source\repos\cloudcache\cloudcache\cloudcache\variadictuple.cpp(49): note: see reference to function template instantiation 'void std::`anonymous-namespace'::HashValueImpl<std::tuple<_Ty>,0>::apply(size_t &,const Tuple &)' being compiled
1> with
1> [
1> _Ty=vecD,
1> Tuple=std::tuple<vecD>
1> ]
1> c:\users\llovagnini\source\repos\cloudcache\cloudcache\cloudcache\variadictuple.cpp(49): note: see reference to class template instantiation 'std::`anonymous-namespace'::HashValueImpl<std::tuple<_Ty>,0>' being compiled
1> with
1> [
1> _Ty=vecD
1> ]
1> c:\users\llovagnini\source\repos\cloudcache\cloudcache\cloudcache\variadictuple.cpp(47): note: while compiling class template member function 'size_t std::hash<_Kty>::operator ()(const std::tuple<_Ty> &) const'
1> with
1> [
1> _Kty=noRef,
1> _Ty=vecD
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xhash(114): note: see reference to function template instantiation 'size_t std::hash<_Kty>::operator ()(const std::tuple<_Ty> &) const' being compiled
1> with
1> [
1> _Kty=noRef,
1> _Ty=vecD
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\type_traits(522): note: see reference to class template instantiation 'std::hash<_Kty>' being compiled
1> with
1> [
1> _Kty=noRef
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(264): note: see reference to class template instantiation 'std::is_empty<_Ty1>' being compiled
1> with
1> [
1> _Ty1=std::hash<noRef>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\unordered_map(23): note: see reference to class template instantiation 'std::_Uhash_compare<_Kty,_Hasher,_Keyeq>' being compiled
1> with
1> [
1> _Kty=noRef,
1> _Hasher=std::hash<noRef>,
1> _Keyeq=std::equal_to<noRef>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xhash(152): note: see reference to class template instantiation 'std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>' being compiled
1> with
1> [
1> _Kty=noRef,
1> _Ty=vecD,
1> _Hasher=std::hash<noRef>,
1> _Keyeq=std::equal_to<noRef>,
1> _Alloc=std::allocator<std::pair<const noRef,vecD>>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\unordered_map(86): note: see reference to class template instantiation 'std::_Hash<std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>>' being compiled
1> with
1> [
1> _Kty=noRef,
1> _Ty=vecD,
1> _Hasher=std::hash<noRef>,
1> _Keyeq=std::equal_to<noRef>,
1> _Alloc=std::allocator<std::pair<const noRef,vecD>>
1> ]
1> c:\users\llovagnini\source\repos\cloudcache\cloudcache\cloudcache\memoization.cpp(28): note: see reference to class template instantiation 'std::unordered_map<noRef,ReturnType,std::hash<_Kty>,std::equal_to<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' being compiled
1> with
1> [
1> ReturnType=vecD,
1> _Kty=noRef,
1> _Ty=vecD
1> ]
1> c:\users\llovagnini\source\repos\cloudcache\cloudcache\cloudcache\memoization.cpp(46): note: see reference to function template instantiation 'std::function<vecD (vecD &)> memoize<vecD,vecD&>(const std::function<vecD (vecD &)> &)' being compiled
1> c:\users\llovagnini\source\repos\cloudcache\cloudcache\cloudcache\helloworld.cpp(38): note: see reference to function template instantiation 'void MultiMemoizator::addFunction<vecD,vecD&>(const MemFunc<vecD (vecD &)> &)' being compiled
1> Exceptions.cpp
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
它在C ++ shell(demo here)上编译,但在MSVC 14上它会返回此错误:
typealias JSONObject = Dictionary<String, AnyObject>
protocol JSONKey {
func toString() -> String
}
extension String: JSONKey {
func toString() -> String { return self }
}
extension Dictionary where Value: AnyObject {
func getInt(JSONKey key: String) -> Int? {
}
mutating func put(key:String, value:AnyObject) {
let Testvalue : NSDictionary = [key: value]
}
}
为什么会这样?