From 60b40de3d8e0ce7ed7ee30e89a2fe1e7c6ff3bef Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sat, 16 Aug 2014 04:54:31 +0200 Subject: [PATCH] construct a trait to detect boost hash compatibility this turns out to be quite tough, since boost::hash just requires a free function 'hash_value' to be "somehow" present, which might be just through ADL. My solution is to inject an fallback declaration of such a function, but only in the namespace where the trait template is defined. Hopefully this never interferes with real hash functions defined for use by boost::hash --- research/try.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/research/try.cpp b/research/try.cpp index b13321066..5939c4f08 100644 --- a/research/try.cpp +++ b/research/try.cpp @@ -124,6 +124,33 @@ namespace std { #include + + +namespace lib { +namespace meta { + + namespace { + struct NoUsableHashDefinition { size_t more_than_one[2]; }; + typedef size_t HasUsableHashDefinition; + + NoUsableHashDefinition hash_value(...); + + } + + template + class provides_BoostHashFunction + { + TY const& unusedDummy = *(TY*)nullptr; + + public: + enum{ value = (sizeof(HasUsableHashDefinition) == sizeof(hash_value(unusedDummy))) }; + }; +}} + +#include + + + //#include #include #include @@ -197,6 +224,8 @@ main (int, char**) << "\n (boost) = " << boo_stringHasher(p) <<"|"<< boo_stringHasher(pp) << "\n custom hash (std) " << std_customHasher(s) <<"|"<< std_customHasher(ss) << "\n custom hash (boost) " << boo_customHasher(v) <<"|"<< boo_customHasher(vv) + << "\n has_boost_hash " << lib::meta::provides_BoostHashFunction::value + << "\n has_boost_hash " << lib::meta::provides_BoostHashFunction::value // << "\n use std from boost: " << std2boo_crossHar(s) <<"|"<< std2boo_crossHar(ss) // << "\n use boost from std: " << boo2std_crossHar(v) <<"|"<< boo2std_crossHar(vv) ;