This page is meant to guide you through the usage of a custom key with a cache.
Key Requirements
- Must have both a move constructor and a move assignment operator.
- Must implement equality operator (
bool operator==(const Self&) const;
).
- Must be hashable by the hasher passed as template parameter to the cache (by default,
absl::Hash<Key>
).
The key does not need to be copyable.
Usage Example
With <tt>absl::Hash</tt> (default hasher)
#include <string>
#include <cachemere.h>
struct ComplexKey {
std::string first;
std::string second;
bool operator==(const ComplexKey& other) const {
return
}
template<typename H> friend H AbslHashValue(H h, const ComplexKey& s)
{
return H::combine(std::move(h), s.first, s.second);
}
size_t capacity() const {
return first.capacity() + second.capacity();
}
};
usig ValueT = int;
ValueT,
With a custom hasher
#include <string>
#include <cachemere.h>
struct ComplexKey {
std::string first;
std::string second;
bool operator==(const ComplexKey& other) const {
return
}
size_t capacity() const {
return first.capacity() + second.capacity();
}
};
struct VeryBadHash {
size_t operator(const ComplexKey&) {
return 0;
}
};
usig ValueT = int;
ValueT,
VeryBadHash>;