How can I create a template class that can understand if type T
is hashable or not, and if it is then use std::unodered_set
to collect elements of type T?. Otherwise, I want it to use std::set
.
This class have the same simple methods as above sets
such as insert(), find()
and so on.
template<class T>
class MySet {
public:
MySet() {}
bool find() {
//some code here
I check if type has operator()
to get information about its "hashability". For this purpose I use the following construction, which I found on this site:
template<typename T>
class HasHash {
typedef char one;
typedef long two;
template<typename C>
static one test(decltype(&C::operator()));
template<typename C>
static two test(...);
public:
enum {
value = sizeof(test<T>(0)) == sizeof(size_t)
};
};
And get is hashable or not (true or false) by typing:
HasHash<HashableType>::value
There is no way for me to use boost library.